Thread safety and Reentrant functions

Source: Internet
Author: User
Tags mutex signal handler

Thread-Safe functions:

When a function is called concurrently by multiple concurrently executing threads, it produces the correct result.


reentrant functions :

Re-entry can be repeated, meaning that the function can be interrupted.

In multi-threaded or exception control flows, when a function runs halfway through, it is possible to be interrupted to execute another function (the function that is paused may be called again), in which case the data or state of the function is not destroyed, the behavior is determined, and in this case the function can be re-entered.

Example: If a function (assuming the Foo () function) receives a signal during execution, it pauses the currently executing function to go to the execution of the signal processing function, and the Foo () function is also called in the signal processing function, so called re-entry occurs. The function is reentrant if the signal handler executes Foo () with the correct result and the Foo () function, which was previously paused, is also executed correctly.



The relationships of reentrant functions and thread-safe functions are:

In general, reentrant functions must be thread-safe, which is thread-safe, but thread-safe functions are not necessarily reentrant functions, such as:


650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7F/C1/wKiom1crLj_Ay6JCAADvTywx0vY295.png "title=" The relationship of thread safety and reentrant functions. PNG "alt=" Wkiom1crlj_ay6jcaadvtywx0vy295.png "/>


Ensure thread safety:

The private space of each thread in a process consists primarily of stacks and registers, and the global and static variables in the process are shared by all threads in the process, so for all threads to access these shared variables, the variables need to be locked to ensure thread safety.


When a function uses a global variable and a static variable, it must not be reentrant or thread-safe, but when we access the global variable and the static variable with a mutex or semaphore lock, it is thread-safe, but it is still not reentrant, because the mutex is generally for different threads of access, A deadlock can occur on the same-thread access.


Global Variables---life cycle and process life cycle, visible domain is global
Static variables---life cycle and process life cycle, visible fields are domains that define variables
member variables---Instances of the same life cycle, the visible domain is inside the class (external calls only indirectly, or cannot be called at all)
Temporary variable---life cycle within the domain where the variable is defined, the Visible field is the field that defines the variable


Make sure you re-enter:

1. Do not use global and static data inside the function;

2. Do not return global and static data (some functions place the result of the calculation in a static structure and return a pointer to the structure.) If we call these functions from a concurrent thread, a disaster can occur because the result being used by one thread is silently overwritten by another thread);

3. The non-reentrant function is not called inside the function;


Explicit reentrant functions: If all the parameters of a function are passed by value (without pointers), and all data references are local automatic stack variables (that is, there is no reference to static or global variables), then the function is to display reentrant, that is, no matter how it is called, we can assert that it is reentrant.

Implicitly reentrant functions: Some parameters in reentrant functions are reference passing (using pointers), that is, when the calling thread is carefully passing pointers to unshared data, it is reentrant.


Summarize:

    1. Determining whether a function is thread-safe is to determine if it can run at the same time as multiple concurrently executing threads, and the output is correct.

    2. Determines whether a function is reentrant, in determining whether the function can be interrupted, and whether the correct result can be output after the recovery is run.

    3. When do we need a reentrant function? Only one function needs to enter more than two times in the same thread, so we need to re-enter the function. These situations are mainly asynchronous signal processing, recursive functions and so on. (The recursive function of a non-reentrant function is not necessarily an error, depending on how you define and use the function). Most of the time, we don't need the function to be reentrant.

    4. The common malloc function is a typical non-reentrant function, but it is a thread-safe function that makes it easy to call malloc at the same time in multiple threads, but it is a dangerous thing to put the malloc function into a signal processing function.



This article is from the "Zero Egg" blog, please be sure to keep this source http://lingdandan.blog.51cto.com/10697032/1770579

Thread safety and Reentrant functions

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.