In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail what the difference between thread safety and reentrant functions is. Xiaobian thinks it is quite practical, so share it with you for reference. I hope you can gain something after reading this article.
Lightweight process (LWP) is a method of multitasking in computer operating systems.
In computer operating systems, lightweight processes (LWP) are a way to achieve multitasking. In contrast to normal processes, an LWP shares all (or most) of its logical address space and system resources with other processes; in contrast to threads, an LWP has its own process identifier, priority, state, and stack and local storage, and has parent-child relationships with other processes; this is the same as processes generated by the Unix-like operating system call vfork(). In addition, threads can be managed by both the application and the kernel, whereas LWP can only be managed by the kernel and scheduled like a normal process. The Linux kernel is a classic example of support for LWP.
In most systems, LWP also differs from normal processes in that it has only a minimal execution context and statistical information required by the scheduler, which is why it is called lightweight. In general, a process represents an instance of a program, and LWP represents the thread of execution of the program (in fact, LWP can easily provide thread implementation when the kernel does not support threads). Because a thread of execution does not require as much state information as a process, LWP does not carry such information.
An important role of LWP is to provide an intermediary system for user-level thread implementations. LWP can obtain kernel services through system calls, so when a user-level thread runs, it only needs to be connected to an LWP to have all the properties of kernel supported threads.
Threads also share the following process resources and environments:
1. file descriptor table
2. Processing method for each signal (SIG_IGN, SIG_DFL or custom signal processing function)
3. current working directory
4. User id and group id
But there are resources that each thread has a share of:
1. thread ID
2. Context, including the values of various registers, program counters, and stack pointers
3. stack space
4. errno variable
5. signal mask word
6. scheduling priority
Processes emphasize exclusivity, while threads emphasize sharing. Thread privacy is most important: hardware context, stack frame.
Multiple threads see a common resource, and if it is not protected, an exception occurs, thread safe (one thread modification affects another thread execution logic exception).
Thread-safe nature: threads share resources, causing some impact.
Another execution flow within the same process: signal processing
Thread Safe:
Thread-safe functions: In C, local variables are allocated on the stack, and any function that does not use static data or other shared resources is thread-safe. Functions that use global variables are not thread-safe.
Functions that use static data or other shared resources must be locked to make the function thread-safe.
Thread-safe:
A function is said to be thread-safe if it can be safely called by multiple threads at the same time.
Thread-safe functions resolve conflicts in accessing shared resources when multiple threads call functions.
Reentrant:
The function is called by different control flows, and it is possible to re-enter the function before the first call returns. This is called reentry.
A function is called a non-reentrant function, whereas a function is called a reentrant function if it accesses only its own local variables or parameters.
3.. Reentrant functions: Functions can be used concurrently by more than one thread without fear of data errors. A reentrant function can be interrupted at any time and resumed later without losing data. Reentrant addresses the determinacy and repeatability of the results of a function run. A reentrant function is a procedure that can be called by multiple tasks without worrying about whether the data will be wrong.
Reentrant functions are written according to the following specifications:
Do not use static or global data inside functions
No static or global data is returned, all data is provided by the caller of the function.
Protect global data using local data or by making local copies of global data.
If global variables must be accessed, use mutual exclusion mechanisms to protect global variables.
5. Do not call non-reentrant functions.
Relationship between the two:
A function that is reentrant for multiple threads is thread-safe.
A function is thread-safe, but not necessarily reentrant. Thread-safe implementation using mutex
Reentrant is stronger than thread safety.
Note: A reentrant function is a function that does not use shared variables internally. Reentrant functions are a proper subset of thread-safe functions.
C language: reentrant function is that when a function is accessed by different threads at the same time, each call uses its own piece of data, and there is no data sharing between multiple calls. Therefore, C language reentrant functions must be thread-safe, because there is no problem of multiple threads sharing a piece of data, and access conflicts will not occur. The requirement for a reentrant function is that the function does not access global or static variables.
Thread safety If there is shared data between multiple calls to the same function by multiple threads, all accesses to the shared data must be serialized, and each thread queues access on a first-come, first-served basis, that is, there is no simultaneous access.
C++: Class member functions are reentrant means that when a class member function is accessed by multiple threads in different instances (one instance per thread), there is no shared data and no data conflict.
Class reentrant means that all member functions of the class are reentrant.
Thread safety of a class means that when the same instance of the class is accessed by multiple threads, the manipulation of the data is serialized and there are no access conflicts.
The difference between the two:
1. The concept of reentrant relates only to the type of variable the function accesses, not to whether locks are used.
2. Thread safety is closely related to the use of locks, and thread safety is often guaranteed by locks.
About "what is the difference between thread safety and reentrant function" this article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.