title: Deleting a daemon thread only after it returns
body: <p>I'm working on a project in which I have a main thread and one daemon thread to perform file outputs. In my main thread I have a field <code>pthread_t * _daemon</code> that I would like to delete, but obviously only after <code>_daemon</code> returns NULL (I understand that using <code>pthread_exit()</code> cause memory leaks).</p>
  
  <p>How can I do it without busy-wait? If I try to use a condition variable I have a problem - When I call <code>signal()</code> form <code>_daemon</code> to wake up my main thread, it deletes <code>_daemon</code> before it <code>_daemon</code> returns NULL.</p>
  
  <p>What I did is to just use a mutex lock that is locked when the program is launched and unlocked before <code>_daemon</code> returns. Is there any better way that would not cause busy wait?</p>