The difference and connection between reentrant function and thread safety

Source: Internet
Author: User



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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.