Thread Cancelling Concept

What do we mean by cancelling a thread?

當我們想要終止一個 thread 時,pthread 提供了一套機制讓我們可以安全地做到這件事而非粗暴的直接關閉,這套機制給 thread 一個機會能先做完善後工作再終止

pthread_cancel

Function Prototype

pthread_cancel(pthread_t tid);

Function

這個函數的作用是「請求」 target thread 終止,但要注意的是我們是「請求」,所以 target thread 能夠自己決定是否要理會這個請求要何時理會這個請求

當我們呼叫這個函數,Kernel 會讓 target thread 表現地像自己呼叫了 pthread_exit(PTHREAD_CANCELED) 一樣

這個函數是 non-blocking,在送出請求後會立即返回


Cancelability State

D-SP-Ch11aja-Cancelability_State

Cancelability Type

D-SP-Ch11ajb-Cancelability_Type

Cancellation Point

D-SP-Ch11ajc-Cancellation_Point