Reentrant and thread security; reentrant thread security

Source: Internet
Author: User

Reentrant and thread security; reentrant thread security

The word thread security is no stranger to me, but when I encounter a word called reentrant function, it gives me the feeling that it is so similar to thread security, however, there must be a difference since we have taken it out. Let's talk about the differences and connections between them.

The two words must be explained first.

Thread security: It seems that the correct option is that thread security issues are caused by global variables and static variables.

Reentrant function: according to my understanding, because different execution streams execute the same function, the execution sequence of the function is different from the expected execution sequence, leading to incorrect execution logic, incorrect results

At this point, the reentrant function does not seem to have much to do with thread security. Here are two typical examples.

Thread security: let's first talk about the environment. The following code indicates the same. both a and B are static variables (that is, they are globally visible), and both sections of code are in the same project.

1 static int a;2 static int b;3 a=10;
//!!!!!!!!4 b=a;
a=5;
//!!!!!!a=b;

It seems that there is no relationship between the two pieces of code. They execute their respective codes, but when two threads (both belong to the same process) run the program simultaneously, the problem arises, that is, the execution sequence of thread A and thread B:

Thread A: a = 10;

Thread B: a = 5;

Thread A: B =;

Thread B: a = B;

Originally, B is equal to 10, but because thread B executes a = 5 first, it leads to an error in programming result 5 of B. This is a simple example of thread security.

 

Reentrant function: Generally, when the program executes a function foo (), it receives a signal, so it suspends the function currently being executed and forwards it to the signal processing function, during the execution of this signal processing function, it will also enter the newly executed function foo (), so that the so-called re-import occurs, so what will the result look like?

When the first insert operation is completed, if an interruption occurs at this time, the system will fall into the kernel. When the system is about to switch from the kernel state to the user State, it will check that there is a signal to be processed, therefore, the signal processing function (signalhandler registered) is called, but insert is called once in the just-registered function, which is equivalent to two people entering a blacklist at the same time, multiple execution streams operate on this function, resulting in Memory leakage and loss of control over node2.

 

In this case, the thread security and reentrant problems are all caused by execution flow problems.

Apparently,If a function is reentrant, it must be thread-safe. However, if a function is thread-safe, it is not necessarily reentrant.As shown in the preceding example, the insert function is thread-safe, but it is not reentrant.

 

So what is the definition of the reentrant function?

In the case of multithreading or abnormal control flow, when a function is running halfway, the control flow (that is, the current command sequence) may be interrupted to execute another function. and "another function" may be itself ., if there is no problem in this case, for example, the data or status will not be damaged, and the behavior is determined. This function is called "reentrant.

What are the conditions for the reentrant function to meet?

  • You cannot use the malloc series functions because the malloc functions are implemented through a global linked list.
  • Standard I/O library functions cannot be called. Many of these library functions are not reentrant.
  • There must be no global or static variables; otherwise, thread security will not be satisfied.

 

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.