Detachstate Attribute

Introduction

就像 process 死亡後會產生 zombie 需要 parent 去 wait 他一樣,thread 在結束後也會留下其 TCB (Thread Control Block) 讓其他 thread 可以知道他已經死了

而 detachstate 就是一個 thread attribute 用來指定要不要 thread 在死亡後留下墓碑

Changing Detachstate Attribute

int pthread_attr_getdetachstate (pthread_attr_t *attr, int *detachstate)
int pthread_attr_setdetachstate (pthread_attr_t *attr, int detachstate)

這兩個函數用來拿現在的 detachstate attribute 以及設定它

detachstate 可以設定以下兩種

PTHREAD_CREATE_DETACHED

thread 所使用的所有資源在它結束運行的那一刻立刻全部清除(包括 TCB)

PTHREAD_CREATE_JOINABLE (default)

會留下墓碑 (TCB) 讓其他 thread join 他

pthread_detach

int pthread_detach(pthread_t tid)

這是第二種改變 detachstate 的方法