linux thread的joinable和detached属性

  • Post author:
  • Post category:linux


linux的thread有两种属性,分别是

joinable和detached。

我们看下man对于pthread_create说明里的notes部分:

A thread may either be joinable or detached.  If a thread is joinable, then another thread can call pthread_join(3) to wait  for  the
       thread  to terminate and fetch its exit status.  Only when a terminated joinable thread has been joined are the last of its resources
       released back to the system.  When a detached thread terminates, its resources are automatically released back to the system:

这两者的区别在于,joinable的thread只有被别的线程执行pthread_join,资源才能被系统回收,另外可以获取该线程的退出码。

detached的thread在退出时,如thread_exit,资源会自动被系统回收。

默认创建的thread都是joinable的,除非在thread_create里特殊指定,或者在thread运行后执行thread_detach,就可以变为detached线程。



版权声明:本文为yuhengyue原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。