title: Wait for a detached thread to finish in C++
body: <p>How can I wait for a detached thread to finish in C++?</p>
  
  <p>I don't care about an exit status, I just want to know whether or not the thread has finished.</p>
  
  <p>I'm trying to provide a synchronous wrapper around an asynchronous thirdarty tool.  The problem is a weird race condition crash involving a callback.  The progression is:</p>
  
  <ol>
  <li>I call the thirdparty, and register a callback</li>
  <li>when the thirdparty finishes, it notifies me using the callback -- in a detached thread I have no real control over.</li>
  <li>I want the thread from (1) to wait until (2) is called.</li>
  </ol>
  
  <p>I want to wrap this in a mechanism that provides a blocking call.  So far, I have:</p>
  
  <pre><code>class Wait {
    public:
    void callback() {
      pthread_mutex_lock(&amp;m_mutex);
      m_done = true;
      pthread_cond_broadcast(&amp;m_cond);
      pthread_mutex_unlock(&amp;m_mutex);
    }
  
    void wait() {
      pthread_mutex_lock(&amp;m_mutex);
      while (!m_done) {
        pthread_cond_wait(&amp;m_cond, &amp;m_mutex);
      }
      pthread_mutex_unlock(&amp;m_mutex);
    }
  
    private:
    pthread_mutex_t m_mutex;
    pthread_cond_t  m_cond;
    bool            m_done;
  };
  
  // elsewhere...
  Wait waiter;
  thirdparty_utility(&amp;waiter);
  waiter.wait();
  </code></pre>
  
  <p>As far as I can tell, this should work, and it usually does, but sometimes it crashes.  As far as I can determine from the corefile, my guess as to the problem is this:</p>
  
  <ol>
  <li>When the callback broadcasts the end of m_done, the wait thread wakes up</li>
  <li>The wait thread is now done here, and Wait is destroyed.  All of Wait's members are destroyed, including the mutex and cond.</li>
  <li>The callback thread tries to continue from the broadcast point, but is now using memory that's been released, which results in memory corruption.</li>
  <li>When the callback thread tries to return (above the level of my poor callback method), the program crashes (usually with a SIGSEGV, but I've seen SIGILL a couple of times).</li>
  </ol>
  
  <p>I've tried a lot of different mechanisms to try to fix this, but none of them solve the problem.  I still see occasional crashes.</p>
  
  <p><strong>EDIT</strong>:  More details:</p>
  
  <p>This is part of a massively multithreaded application, so creating a static Wait isn't practical.</p>
  
  <p>I ran a test, creating Wait on the heap, and deliberately leaking the memory (i.e. the Wait objects are never deallocated), and that resulted in no crashes.  So I'm sure it's a problem of Wait being deallocated too soon.</p>
  
  <p>I've also tried a test with a <code>sleep(5)</code> after the unlock in <code>wait</code>, and that also produced no crashes.  I hate to rely on a kludge like that though.</p>
  
  <p><strong>EDIT</strong>:  ThirdParty details:</p>
  
  <p>I didn't think this was relevant at first, but the more I think about it, the more I think it's the real problem:</p>
  
  <p>The thirdparty stuff I mentioned, and why I have no control over the thread:  this is using CORBA.</p>
  
  <p>So, it's possible that CORBA is holding onto a reference to my object longer than intended.</p>