Defined
(1). Thread-safe functions: Generally speaking, a function is called thread-safe, and when it is called repeatedly by multiple concurrent threads , it always produces the correct result.
(2). Reentrant: When the program executes to a function foo (), it receives a signal, pauses the currently executing function, goes to the signal processing function, and the signal processing function executes, and it goes to the function foo () just executed, so called re-entry occurs. if Foo () is able to run correctly, and when the processing is done, the previously paused foo () will be able to run correctly, which means it is reentrant.
(3). Expand:
1). If a function uses a global or static variable, then it is not thread-safe or reentrant;
2). If we improve it and lock it with a mutex or semaphore when accessing a global or static variable, you can make it thread-safe, but at this point it is still non-reentrant because the usual locking method is for access to different threads, and there may be problems with the same thread;
3). If you remove the global or static variables from the function and change them to other forms such as function parameters, it is possible to make the function both thread-safe and reentrant.
-
Contact
reentrant function is a thread-safe function, but in turn, a thread-safe function may not be a reentrant function.
Difference
(1) Solve the problem:
A. reentrant functions the problem to solve is that you do not use static or global data inside the function, do not return static or global data, and do not call the non-reentrant function.
B. Thread-safe functions to solve the problem is that multiple threads invoke a function when accessing a resource conflict.
(2) Measures to ensure
A. The measures to ensure thread safety are: thread-safe functions do not use shared data (global, static, or heap) or implement synchronization mechanisms to protect shared data.
B. Guaranteed reentrant measures: do not share data and do not call non-reentrant functions.
(1) Do not use static variables and global variables, persist in using only local variables;
(2) If the global variable must be accessed, the global variable can be protected by mutual-exclusion semaphore;
(3) Get to know which system calls are reentrant, and use secure system calls in the multitasking process;
(4) Do not call any other non-reentrant functions;
(5) Use the stack malloc/new with caution.
(3) Change
If you use a static variable, the function can be turned into a thread-safe function by locking it, but it still may not be reentrant, such as Strtok. Strtok is neither reentrant nor thread-safe. Locked strtok are not reentrant, but thread-safe. And Strtok_r is both reentrant and thread-safe.
4. Reason for thread insecurity and non-reentrant function
(1) the root cause of any thread insecurity problem is "shared data." Therefore, a function that does not use any shared data (that is, reentrant functions) is certainly thread-safe.
(2) the reason for non-reentrant functions is:
A. They are known to use static data structures
B. They call malloc and free.
because malloc typically maintains a linked table for the allocated store, the process may be modifying this linked table while inserting the execution signal handler function.
c. They are standard IO functions.
because many implementations of the standard IO library use global data Structures
(3) thread-safe functions may not necessarily be heavy Into the function, because guaranteed correctness .
The shared data can be:
function to place the returned result in a common position
A pointer variable or reference variable shared between threads passed in by the caller
Shared static variables that are inherently used inside a function
4. Supplement
(1) common non-reentrant functions are:
free --------Global memory allocation table
(2) Example of non-reentrant solution (printf)
For example, the program is calling printf output, but when you call printf, a signal appears, and the corresponding signal handler has a printf statement, which results in a mix of two printf outputs.
if it is for printf locking, the same situation as above will lead to deadlock. In this case, the method used is usually to shield certain signals in a particular area.
method of Shielding the signal:
1> Signal (sigpipe, sig_ign);//Ignoring some signals
2> sigprocmask ()
Sigprocmask is defined only for single threads
3> pthread_sigmask ()
Pthread_sigmasks can be used in multiple threads
This article is from the "sunshine225" blog, make sure to keep this source http://10707460.blog.51cto.com/10697460/1770763
Thread safety and Reentrant functions