The connection and difference between thread safety and reentrant function

Source: Internet
Author: User

lightweight process (LWP) is Computer operating System one way to implement multitasking.

logical address space thread () produces the same process. In addition, thread kernel management, while LWP can only be managed by the kernel and dispatched like a normal process. The Linux kernel is a typical example of support for LWP.

in most systems, the difference between an LWP and a normal process is that it has only a minimal execution context and the statistics required by the scheduler, which is why it is called lightweight. In general, a process represents an instance of a program, and the LWP represents the execution thread of the program (in fact, LWP can easily provide thread implementations when the kernel does not support threads). Because an execution thread does not require so much state information as a process, the LWP does not carry such information.

An important role of LWP is to provide an intermediate system for user-level threading implementations. LWP can obtain the service provided by the kernel through system calls, so when a user-level thread runs, it can have all the properties of the kernel support thread simply by connecting it to an LWP.

Each thread also shares The following process resources and environment:
1. The textItem Descriptor
2. how each signal is processed (sig_ign, SIG_DFL, or custom signal processing functions )
3. Current Working directory
4. user ID and group ID

But some resources are per threadeach with a copyOf:
1.Threadid
2. 3. stack space
4. Errno 5. signal shielding word
6.

The process emphasizes exclusivity, while threads emphasize sharing. The most important thing about thread private is: hardware context, stack frame.

Multiple threads see public resources, and if not protected, an exception occurs, thread safety (one thread modification affects another thread executing a logical exception).

Creating Thread-safe nature: threads share resources, causing some impact.

Another execution flow within the same process: signal processing

Thread Safety:

Thread-safe functions: In the C language, local variables are allocated on the stack, and any functions that do not use static data or other shared resources are thread-safe. Functions that use global variables are non-thread safe.
A function that uses static data or other shared resources must be locked to make the function thread safe.

Thread-Safe:
If a function can be called safely by multiple threads at the same time, it is called a thread-safe function.
Thread-Safe functions resolve conflicting issues when multiple threads call a function when accessing a shared resource.

Can be re-entered:

    1. Functions are called by different control processes, and it is possible to enter the function again when the first call is not returned, which is called re-entry.

    2. Access can be caused by re-entry, such as a function called non-reentrant function, conversely, if a function only accesses its own local variables or parameters, it is called reentrant function.

3. reentrant function Feature: Functions can be used concurrently by more than one thread, without worrying about data errors. The Reentrant function can be interrupted at any time and then continue running without losing data. reentrant resolves the certainty and repeatability of the result of a function run. a reentrant function is a process that can be called by multiple tasks, and the task does not have to worry about whether the data will go wrong when it is called.

The reentrant functions are written in the following specification:

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. If you must access global variables, use mutually exclusive mechanisms to protect global variables.
5, do not call the non-reentrant function.

The relationship between the two:
1. A function is thread-safe for multiple threads to be reentrant.
2, a function is thread-safe, but it is not necessarily reentrant. "Thread Safety with mutex"
3. The reentrant sex is stronger than thread safety.

Note: a reentrant function is a function in which shared variables are not used internally. A reentrant function is a true subset of thread-safe functions.

C: reentrant function means that when a function is accessed by a different thread at the same time, each invocation uses its own data alone, and there is no data sharing between multiple invocations. So the C-reentrant function must be thread-safe, because there are no more threads sharing a single data problem, and no access violation occurs. The requirement of reentrant functions is that the function does not access global or static variables.

Thread safety if more than one thread has shared data between multiple invocations of the same function, all access to the shared data must be serialized, and each thread is queued for access by first served, that is, there is no simultaneous access.

In C + +: A member function of a class can be re-entered by saying that when a member function of a class is accessed by multiple threads in a different instance (one instance per thread), there is no shared data and there is no data conflict.

All member functions of a class that are reentrant represent a class are reentrant.
The thread safety of a class is that when the same instance of a class is accessed by multiple threads, the manipulation of the data is serialized and there is no access violation.

The difference between the two:

1. The reentrant concept is only related to the type of variable the function accesses, and whether or not a lock is used.
2. Thread safety and lock usage are closely related, and many times thread safety is ensured by locks.

This article is from the "Small Stop" blog, please be sure to keep this source http://10541556.blog.51cto.com/10531556/1770410

The connection and difference between thread safety and reentrant function

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.