tags:

views:

577

answers:

1

How do I determine if a detached pthread is still alive ?

I have a communication channel with the thread (a uni-directional queue pointing outwards from the thread) but what happens if the thread dies without a gasp?

Should I resign myself to using process signals or can I probe for thread liveliness somehow?

+6  A: 

You can use pthread_kill like this:

int ret = pthread_kill(YOUR_PTHREAD_ID, 0);

If you get a ESRCH value, it might be the case that your thread is dead.

Pablo Santa Cruz
Short and sweet... thanks!
jldupont
The problem with that is the `YOUR_PTHREAD_ID` might have been recycled for another thread in the mean time since it was detached. So it should rather be: __If you get ESRCH, your thread is dead, otherwise you can't be sure__ (unless you know the ID's of newly created threads).
RedGlyph
@RedGlyph: this is a very typical case of identifier recycling conundrum. In my case, I am willing to live with the small probability of collision it incurs because I'll be polling on a reasonable frequency.
jldupont