Steven Behnke
Steven Behnke

Reputation: 3344

pthread thread state

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?

Upvotes: 3

Views: 8657

Answers (2)

Steven Behnke
Steven Behnke

Reputation: 3344

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

Upvotes: 2

Jonathan Graehl
Jonathan Graehl

Reputation: 9301

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.

Upvotes: 5

Related Questions