Thread Safety: When multiple threads access the same zone, the end result is predictable, and the result is not unpredictable because of a conflict or an abnormal outage
1. Re-entry: The function is called by different control processes, and it is possible to enter the function again when the first call has not yet returned, which is called re-entry;
2. Non-reentrant function: When accessing a global variable or parameter, it is possible that the re-entry causes confusion, such as a function called a non-reentrant function
A function that is non-reentrant if one of the following conditions is met
(1) Malloc/free is called because malloc manages the heap with a global list.
(2) Call the standard I/O card function, Biaozhun I/O library functions Many implementations are using global data structures in a non-reentrant manner
3. reentrant function: If a function accesses only its own local variables or parameters, it is called a reentrant function
Reentrant functions can be called by more than one task, without worrying about data corruption, reentrant functions can be interrupted at any time, and can be run after a period of time, and the corresponding data will not be lost; reentrant functions or only local variables, either stored in registers or stacks, or using global variables. You want to protect the global variables
4. The difference and connection between reentrant function and thread safety
(1) Thread safety is raised in the case of multiple threads, while reentrant functions can be used in the case of only one thread;
(2) Thread safety must not be reentrant, reentrant functions must be thread safe
(3) If there is a global variable in a function, then this function is neither thread-safe nor reentrant.
(4) If access to the critical resource is added to the lock, the function is thread-safe
(5) If the data in a function is full of its own stack space, then this function is thread-safe and reentrant.
(6) Thread-safe functions enable different threads to access the same piece of address space, while reentrant functions require different execution streams to operate independently of the data to make the results the same
The difference and connection between reentrant function and thread safety