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.

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