title: Can't pass a local variable from a thread
body: <p>I made a thread <em>thread1</em> which creates another thread <em>thread2</em>. When I try to pass the value of <em>fd</em> (fd declared inside <em>thread2</em>) using pthread_exit from <em>thread2</em> to <em>thread1</em> I get a garbage value in <em>retval</em>. But when I declare fd as a global variable I get correct value in <em>retval</em>. I got an answer that this is because the thread is finished so it can't pass the value. But in case of <em>functions</em>, a local variable's scope is also limited inside the function and they do return values. So why can't a thread do that? </p>
  
  <p>Here is the code that I tried:</p>
  
  <pre><code>    void *thread2(void *message)
      {
          int fd;
          void *retval;
          fd=open(message,O_RDWR);
          printf("message is - %s",(char *)message);
          pthread_exit(&amp;fd);
      }
      void *thread1(void *message)
      {
          void *retval;
          pthread_t *tid2;
          tido=malloc(sizeof(pthread_t));
          pthread_create(tid2,NULL,thread2,message);
          pthread_join(*tid2,&amp;retval);
          printf("fd in write is-%d\n",*(int *)retval);
          pthread_exit(&amp;retval);
      }
  </code></pre>