Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if a pthread died

Tags:

pthreads

How can one know if a pthread died?

Is there a way to check a pthreads status?

like image 593
some_id Avatar asked Apr 23 '11 11:04

some_id


People also ask

Can pthread_create fail?

The pthread_create() function will fail if: [EAGAIN] The system lacked the necessary resources to create another thread, or the system-imposed limit on the total number of threads in a process PTHREAD_THREADS_MAX would be exceeded.

What happens if pthread_join is not called?

After a successful call to pthread_join(), the caller is guaranteed that the target thread has terminated. Joining with a thread that has previously been joined results in undefined behavior. Failure to join with a thread that is joinable (i.e., one that is not detached), produces a "zombie thread".

How do you end a pthread?

The pthread_exit() function terminates the calling thread, making its exit status available to any waiting threads. Normally, a thread terminates by returning from the start routine that was specified in the pthread_create() call which started it.


2 Answers

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?

like image 115
Damon Avatar answered Sep 19 '22 15:09

Damon


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).

like image 33
evnu Avatar answered Sep 18 '22 15:09

evnu