title: Why does pthread_exit() in rare cases cause a SEGV when called after pthread_detach()?
body: <p>I am getting a SEGV in C++ that I cannot easily reproduce (it occurs in about one in 100,000 test runs) in my call to <code>pthread_join()</code> as my application is shutting down.  I checked the value of errno and it is zero.  This is running on Centos v4.</p>
  
  <p>Under what conditions would <code>pthread_join()</code> get a SEGV? This might be some kind of race condition since it is extremely rare.  One person suggests I should not be calling pthread_detach() and pthread_exit(), but I am not clear on why.</p>
  
  <p>My first working hypothesis was that <code>pthread_join()</code> is being called while <code>pthread_exit()</code> is still running in the other thread and that this somehow leads to a SEGV, but many have stated this is not an issue.</p>
  
  <p>The failing code getting SEGV in the main thread during application exit looks roughly like this (with error return code checking omitted for brevity):</p>
  
  <pre><code>// During application startup, this function is called to create the child thread:
  
  return_val = pthread_create(&amp;_threadId, &amp;attr,
                              (void *(*)(void *))initialize,
                              (void *)this);
  
  // Apparently this next line is the issue:
  return_val = pthread_detach(_threadId);
  
  // Later during exit the following code is executed in the main thread:
  
  // This main thread waits for the child thread exit request to finish:
  
  // Release condition so child thread will exit:
  releaseCond(mtx(), startCond(), &amp;startCount);
  
  // Wait until the child thread is done exiting so we don't delete memory it is
  // using while it is shutting down.
  waitOnCond(mtx(), endCond(), &amp;endCount, 0);
  // The above wait completes at the point that the child thread is about
  // to call pthread_exit().
  
  // It is unspecified whether a thread that has exited but remains unjoined
  // counts against {PTHREAD_THREADS_MAX}, hence we must do pthread_join() to
  // avoid possibly leaking the threads we destroy.
  pthread_join(_threadId, NULL); // SEGV in here!!!
  </code></pre>
  
  <p>The child thread which is being joined on exit runs the following code which begins at the point above where <code>releaseCond()</code> is called in the main thread:</p>
  
  <pre><code>// Wait for main thread to tell us to exit:
  waitOnCond(mtx(), startCond(), &amp;startCount);
  
  // Tell the main thread we are done so it will do pthread_join():
  releaseCond(mtx(), endCond(), &amp;endCount);
  // At this point the main thread could call pthread_join() while we 
  // call pthread_exit().
  
  pthread_exit(NULL);
  </code></pre>
  
  <p>The thread appeared to come up properly and no error codes were produced during its creation during application startup and the thread performed its task correctly which took around five seconds before the application exited.</p>
  
  <p>What might cause this rare SEGV to occur and how might I program defensively against it.  One claim is that my call to pthread_detach() is the issue, if so, how should my code be corrected.</p>