Reputation: 29886
How can one know if a pthread died?
Is there a way to check a pthreads status?
Upvotes: 9
Views: 15689
Reputation: 70126
if(pthread_kill(the_thread, 0) == 0)
{
/* still running */
}
See: pthread_kill
Note: there is an inherent risk to using pthread_kill() to test if a thread is still running. See this post for an explanation: How do I determine if a pthread is alive?
Upvotes: 11
Reputation: 41
I want to add to the discussion the fact that a thread can just die in another case that is not mentioned here in the case of a signal such as SIGPIPE when it is not handled by the hosting process or the thread it self and in situations when such a signal can arise
Upvotes: 4
Reputation: 6680
If you don't need to write a portable application and can use GNU extensions, you can use pthread_tryjoin_np
. I believe there isn't another way to do it, except for setting up communication between two threads (e.g. using a global mutex which is hold by a thread as long as it is alive).
Upvotes: 4