Friday, October 31, 2008

Tech: How to find the existence of a thread

To find if a thread created using pthread_create exists or not use phread_kill with signal number 0
on the thread that needs to be tested. If the return value is 0, then the thread is alive.
If the return value is ESRCH, then the thread is not alive in the system.

Sample code may look like:

bool is_thread_alive(pthread_t thread_id)
{
int status = pthread_kill(thread_id, 0);
if (status == ESRCH)
return true;
else
return false;
}