Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pthread thread state

Tags:

c++

c

pthreads

Is there a mechanism that I can use to tell if a pthread thread is currently running, or has exited? Is there a method for pthread_join() that is able to timeout after a specific period of time if the thread has not yet exited?

like image 829
Steven Behnke Avatar asked Apr 27 '26 04:04

Steven Behnke


2 Answers

If you're only targeting linux, use http://www.kernel.org/doc/man-pages/online/pages/man3/pthread_tryjoin_np.3.html

If you need something for any POSIX system, you can copy the "pthread_timedjoin" implementation in http://www.opengroup.org/onlinepubs/000095399/xrat/xsh_chap02.html#tag_03_02_08_21 - which uses a condition variable to signal thread termination, and pthread_cond_timedwait for the timeout.

like image 114
Jonathan Graehl Avatar answered Apr 29 '26 18:04

Jonathan Graehl


I just ended up wrapping the thread in a C++ class and keeping a state variable around that could be checked later.

like image 36
Steven Behnke Avatar answered Apr 29 '26 18:04

Steven Behnke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!