title: pthread_exit vs. return
body: <p>I have a joinable pthread runner function defined as below:</p>
  
  <pre><code>void *sumOfProducts(void *param)
  {
  ...
  pthread_exit(0);
  }
  </code></pre>
  
  <p>This thread is supposed to join the main thread.</p>
  
  <p>Whenever I ran my program through Valgrind I would get the <strong>following leaks</strong>:</p>
  
  <pre><code>LEAK SUMMARY:
     definitely lost: 0 bytes in 0 blocks
     indirectly lost: 0 bytes in 0 blocks
       possibly lost: 0 bytes in 0 blocks
     still reachable: 968 bytes in 5 blocks
          suppressed: 0 bytes in 0 blocks
  
  ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 15 from 10)
  </code></pre>
  
  <p>I checked the man page for pthreads which said:</p>
  
  <pre><code>  The new thread terminates in one of the following ways:
  
     * It  calls  pthread_exit(3),  specifying  an exit status value that is
       available  to  another  thread  in  the  same  process   that   calls
       pthread_join(3).
  
     * It  returns  from  start_routine().   This  is  equivalent to calling
       pthread_exit(3) with the value supplied in the return statement.
  
     * It is canceled (see pthread_cancel(3)).
  
     * Any of the threads in the process calls exit(3), or the  main  thread
       performs  a  return  from main().  This causes the termination of all
       threads in the process.
  </code></pre>
  
  <p>Miraculously, when I replaced the pthread_exit() with a return statement, <strong>the leaks disappeared</strong>.</p>
  
  <pre><code>return(NULL);
  </code></pre>
  
  <p>My actual question is three-pronged:</p>
  
  <ol>
  <li>Can someone explain why the return statement gave no leaks?</li>
  <li>Is there some fundamental difference between both statements, in relation to exiting from threads? </li>
  <li>If so, when should one be preferred over the other? </li>
  </ol>