pthread thread state

℡╲_俬逩灬. 提交于 2020-01-13 05:16:24

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/1365711/pthread-thread-state

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!