Dead Threads - to join or not to join (detatch)

Greetings all!

I have an application that periodically creates a helper thread to complete some actions while the main thread sits and waits for new input, upon which it might spawn yet another helper thread to act upon that input.

So I have helper threads coming and going while the main thread is persistant.

On a whim I invoked the [pidin] command to try it out. I found a bunch of DEAD threads.

I looked at the QNX documentation and found the following:

The thread has terminated and is waiting for a join by another thread.

I also went back and reviewed my “Pthreads Programming” book and it talks about pthread_join as a way for one thread to block until the other thread has completed, and returned its exit value. Then there is another section that indicates that if we don’t really care about a threads return value, that we should consider creating the thread with it in the detatched state.

This all sounds simple enough. But I have questions.

What would happen if I decided not join or detach these transient threads? Would I eventually consume all of my resources, which would then cause the application to fail ins some unknown manner? (I am envisioning dead threads like zombie processes).

Is this just a matter of “good housekeeping”, something that we should just always do? (Ignore the man behind the curtain.)

Is there anything I should be aware of when creating a detatched thread? I mean other than the fact that we cannot get a detatched threads exit value.

Thanks in advance,


There are definitely resources attached to the threads that are hanging around, although not much. There is a risk that you run out of thread identifiers if you are frequently spawning threads and having them terminate in a non-joined state.

Basic rule of thumb … create the thread detached if you never plan on waiting for it to return.

Of course there is a corollary to this, make sure you synchronize your thread terminations with your program activity properly. In general people spawn threads to “do something” and eventually to come back into some serial stream of execution. The pthread_join() with a non-detached thread is a “built-in” synchronization mechanism that means that you may be able to avoid mutexes, condvars or some other way of making sure that your program continues on with the data that the created thread generated.

Personally, I create detached threads in thread pool situations where I need other infrastructure and I use pthread_join for everything else.