reentrant,thread-safe 和 async-signal-safe

可重入,線程安全和異步信號安全POSIX定義:

Reentrant Function

A function whose effect, when called by two or more threads, is guaranteed to be as if the threads each executed the function one after another in an undefined order, even if the actual execution is interleaved.

Thread-Safe

A function that may be safely invoked concurrently by multiple threads. Each function defined in the System Interfaces volume of IEEE Std 1003.1-2001 is thread-safe unless explicitly stated otherwise.

Async-Signal-Safe Function

A function that may be invoked, without restriction, from signal-catching functions. No function is async-signal-safe unless explicitly described as such.

通俗地講,用相同的輸入,每次調用函數一定會返回相同的結果。這就是可重入

嚴格地講

* Must hold no static (global) non-constant data.
* Must not return the address to static (global) non-constant data.
* Must work only on the data provided to it by the caller.
* Must not rely on locks to singleton resources.
* Must not call non-reentrant computer programs or routines.

線程安全僅要求了可以安全地被線程併發執行

因爲它內部可以訪問全局變量或靜態變量,不過需要加鎖,也就是說,只要是在線程可控之中的,每次調用它返回不同的結果也沒關係。到這裏我們可以看出:可重入函數一定是線程安全的,而反之未必。

異步信號安全,它其實也很簡單,就是一個函數可以在信號處理函數中被安全地調用。看起來它似乎和線程安全類似,其實不然,我們知道信號是異步產生的,但是,信號處理函數是打斷主函數(相對於信號處理函數)後執行,執行完後又返回主函數中去的。也就是說,它不是併發的!

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