Writing Reentrant and Thread-Safe Code
http://www.navo.hpc.mil/cgi-bin/search/search.pl?q=index&showurl=%2Fusersupport%2FIBM%2Faixprggd%2Fgenprogc%2Fwriting_reentrant_thread_safe_code.htm
http://publib.boulder.ibm.com/infocenter/systems/index.jsp?topic=/com.ibm.aix.genprogc/doc/genprogc/writing_reentrant_thread_safe_code.htm
http://blog.csdn.net/lovekatherine/archive/2007/03/28/1544585.aspx
Reentrance
A reentrant function does not hold static data over successive calls, nor does it return a pointer to static data. All data is provided by the caller of the function. A reentrant function must not call non-reentrant functions.Making a function reentrant
- returning dynamically allocated data
- use caller-provided storage
Thread-Safety
A thread-safe function protects shared resources from concurrent access by locks. Thread-safety concerns only the implementation of a function and does not affect its external interface.Making a function thread-safe
In C, local variables are dynamically allocated on the stack. Therefore, any function that does not use static data or other shared resources is trivially thread-safe.
- locking shared resources
- workaround
- use a global lock on the library
- use a lock for each library component
Converting Libraries
- exported global variables
should be encapsulated, make private (static), and accessed by subroutines. - static variables
protect shared resource by locks. - make non-reentrant function reentrant
- make thread-unsafe function thread-safe
My Understanding
if Reetrant ==> thread-safe
if thread-safe 不一定是 reetrant
沒有留言:
張貼留言