linux thread的joinable和detached屬性

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線程。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章