reentrant & thread safe with examples

Wiki says, In computing, a computer program or subroutine is called reentrant if multiple invocations can safely run concurrently. The concept applies even on a single processor system, where a reentrant procedure can be interrupted in the middle of its execution and then safely be called again ("re-entered") before its previous invocations complete execution.

強調兩個點:1)reentrant強調的是procedure被中斷,並且同一個procedure又被調用;2)強調safely run,兩個方面:a. 可以運行(run),也就是不能被block(例如,一個procedure拿着鎖lock被中斷了,同一個procedure又被調用的時候就會因爲拿不到鎖lock被block,也就是不可重入);b. 所謂safely是指每次procedure調用都應該產生正確的結果。

不強調concurrently,也就是說並不強調多個調用同時運行,只強調一個procedure被中斷,並且同一個procedure又被調用。concurrently是在thread safe中強調的。

Wiki says, Thread safety is a computer programming concept applicable to multi-threaded code. Thread-safe code only manipulates shared data structures in a manner that ensures that all threads behave properly and fulfill their design specifications without unintended interaction.

強調的是,1)多個thread同時運行(concurrently),2)並且結果正確。

Why are malloc() and printf() said as non-reentrant? See excellent answer in stackoverflow.

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