2017年8月25日 星期五

pthread cancel

pthread_cancel() 送取消請求給 thread,thread 依據其取消狀態 (cancellation state) 及取消 type 因應。

thread 的取消狀態有啟用 (預設) 和停用,可由 pthread_setcancelstate() 設定。停用時收到取消請求暫不處理 (queued),直到啟用。收到取消請求且啟用時,type 決定取消何時進行。

thread 的取消 type 有 deferred (預設) 及 asynchronous,可由 pthread_setcanceltype() 設定。Asynchronous 表示可在任何時間點取消,但系統不保證立即進行。Deferred 表示直到 thread 呼叫到含有取消點 (cancellation point) 的函數才進行取消動作。

取消動作依序包括:
  1. pop 每個 Cancellation clean-up handlers 並執行。(見 pthread_cleanup_push())
  2. 呼叫 thread-specific data destructors (見 pthread_key_create())
  3. thread 結束 (見 pthread_exit())
pthread_cancel() 回傳值 0 表示成功送出請求。用 pthread_join() 得到 thread 的 exit status 是 PTHREAD_CANCELED 才能知道取消動作完成。

Linux 用 signal 實作 thread 取消,在 NPTL 是第一個 real-time signal (也就是 signal 32)。

pthread_cancel() MT-safe

async-cancel-safe 函數:能夠在啟用 asynchronous 取消的應用安全使用的函數,包括 pthread_cancel()、pthread_setcancelstate()、pthread_setcanceltype()。

必須含有取消點的函數:包括 sleeps() 等,列表見 man pthreads
  • pthread_testcanel():測試是否取消
可能含有取消點的函數:列表見 man pthreads

應用:有些 thread 設計是無窮的迴圈,其它 thread 呼叫 pthread_cancel() 作為結束。

參考來源
  1. man pthread_canel
  2. man pthreads

沒有留言:

張貼留言

SIP header Via

所有 SIP 訊息 都要有 Via,縮寫 v。一開始的 UAC 和後續途經的每個 proxy 都會疊加一個 Via 放傳送的位址,依序作為回應的路徑。 格式 sent-protocol sent-by [ ;branch= branch ][ ; 參數 ...] s...