title: Implementing a user level thread library - return value from makecontext
body: <p>I've seen a few questions on user thread libraries, but none that seem to answer my question.  I am able to create threads, run them, cancel them, and exit from them.  What I cannot do for some reason is get a thread to return data.</p>
  <p>When I initialize my thread library, I set my exit thread context as follows:</p>
  <pre><code>getcontext(&amp;threadEnd);
  threadEnd.uc_stack.ss_sp = (char *)malloc(SIGSTKSZ);
  threadEnd.uc_stack.ss_size = SIGSTKSZ;
  makecontext(&amp;threadEnd, (void *) thread_exit, 1, &amp;ReturnValue);
  </code></pre>
  <p>I create a thread and assign it as follows:</p>
  <pre><code>thread-&gt;myContext.uc_stack.ss_sp = (char *) malloc(SIGSTKSZ);
  thread-&gt;myContext.uc_stack.ss_size = SIGSTKSZ;
  thread-&gt;myContext.uc_link = &amp;threadEnd;
  </code></pre>
  <p>When the function returns and <em>thread_exit()</em> is called:</p>
  <pre class="lang-c prettyprint-override"><code>    void thread_exit(void* retval){
      int* test;
      if (retval != NULL)
      {
          test = (int*) retval;
          printf(&quot;Returned value: %i\n&quot;, *test);
          fflush(stdout);
      }
  </code></pre>
  <p>The printout is always &quot;Returned value: 0&quot;</p>
  <p>The called function is returning a pointer to an integer.</p>
  <p>What am I doing wrong?</p>