쓰레드 관련 질문 ( 인자 넘기기 )

meconfidence의 이미지

main -> function(쓰레드생성 & detach) -> 쓰레드
로 인자를 넘기는 데,
usleep ( or sleep )을 주지 않으면 인자가 잘 안넘어가더라구요..
인자가 잘 안넘어 가는 이유를 알고싶고, sleep을 쓰지 않는 다른 방법이 있으시면 가르켜주세요.(단 join이 아닌 detach로 해야만 합니다.)
다음은 수도 코드 입니다.
가르켜 주시면 한수 배우겠습니다. :D

main()
{
    char *a[2] = {"asdf","1234"};
    char *b[2] = {"bbbb","0000"};
    function(a,b);
    ...
    ...
}

function(a,b)
{
    pthread_t thread_id;
    struct Thread_arg thread_arg
    thread_arg.parameter1 = a;
    thread_arg.parameter2 = b;

    pthread_create(&thread_id , NULL, thread_f,&thread_arg); 
    usleep(1000); // 여기에 넣거나,
    pthread_detach(thread_id );
    // usleep(1000); // 혹은 여기에 넣었습니다.
}

void * thread(void * arg)
{
    struct * Thread_arg = (struct Thread_arg *)arg;
    char * a = thread_arg->parameter1;
    char * b = thread_arg->parameter2;
    ...
}
dudungsil의 이미지

char* Foo ()
{
    char dataOnStack[] = "I'mVolatile" ;
    return dataOnStack ;
}

int main (void)
{
    char* stupidJob = Foo ();
    printf ("%s\n", stupidJob);
    return 0;
}

산넘어 산

meconfidence의 이미지

알아 냈어요..^^;;

main -> function -> thread 에서,
인자가 넘어가는 과정을 보면, 중간에 function에서 로컬 변수 하나를 만든 다음에, thread를 호출하는데...
function이 쓰레드를 만들고, detach로 만들고 끝나면서,로컬 변수가 죽게 되죠.
thread에서는 function의 로컬변수를 call by reference로 받게 되는데, 그 변수가 죽게 되어서 참조할수가 없게 되는겁니다. :wink:

항상 감사하는 마음으로...

meconfidence의 이미지

dudungsil 님 감사합니다.
역시 기본이 중요한듯 하네요... :)

항상 감사하는 마음으로...

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.