reentrant functions and thread safety issues

Source: Internet
Author: User

Thread-Safe functions

ensure thread safety:
to ensure functional thread safety, it is important to consider shared variables between threads. Different threads that belong to the same process share the global extents and heaps in the process memory space, while the private thread space mainly includes stacks and registers. Therefore, for different threads of the same process, each thread's local variables are private, while global variables, local static variables, and variables allocated to the heap are shared. when accessing these shared variables, if you want to ensure thread safety, you must pass the lock method.
Consequences of Thread insecurity:
The consequences of thread insecurity can be obvious--the value of a shared variable can cause unpredictable changes due to the access of different threads, resulting in program errors or even crashes.

reentrant functionsConcept:

For example: The main function and the signal processing function call a same function (in a global linked list in the head of a node), the main function is first inserted, the operation process is divided into two steps,p->next=head;head=p; when the first step after the completion of the operation may be suddenly interrupted, The process from the user state switch to the kernel state, again back to the user state to detect the signal to be processed, and then switch to the user state to perform the signal processing function, and the function of the head inserted a node, the insertion node is returned to the kernel state, The second step continues from the bottom of the call to the insertion function from the kernel return to the main function, and for the same linked list, only one node can be inserted at a time, and the result can only be inserted into one node and the other discarded.

Therefore, the insertion function is called by a different control flow, it is possible to enter the function again before the first call is returned, so it is called a reentrant function.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7F/CB/wKioL1ctgfqy2c9QAACSeIwSbwI363.png "title=" QQ picture 20160507134556.png "alt=" Wkiol1ctgfqy2c9qaacseiwsbwi363.png "/>       
Make sure you re-enter:
to ensure that the function is reentrant, the following conditions are met:
1. Do not use static or global data inside the function
2. Do not return static or global data, all data is provided by the caller of the function.
3. Use local data, or protect global data by making local copies of global data.
4, do not call the non-reentrant function.

Ensure non-reentrant:

1. malloc or free is called because malloc also manages the heap with a global list.

2. Standard I/O library functions are called. Many implementations of the standard I/O library use global data structures in a non-reentrant manner.

Consequences of non-reentrant:
the consequences of non-reentrant are mainly reflected in cases where the signal processing function is required to be re-entered. If a function that is not reentrant is used in the signal processing function, it can cause a program error or even crash.

The difference between the reentrant function and the thread safety function:

1. reentrant functions are one of the thread-safe functions, characterized in that they are called by multiple threads and do not reference any shared data, that is, static or global variables.
2. reentrant functions are generally more efficient than non-reentrant thread-safe functions because they do not require synchronous operations.

3.A function is thread-safe for multiple threads to be reentrant.
4. A function is thread-safe, but not necessarily reentrant. "Thread Safety with mutex"

reentrant functions and thread safety issues

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.