In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what are the characteristics of multithreading concurrency". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what are the characteristics of multithreading concurrency.
Multithreading and concurrency
Multithreading is still mainstream in server programs developed using C++. Generally speaking, there is a thread pool to deal with received requests, which can effectively provide the concurrency ability of the server and the utilization of CPU.
However, multithreading is also a double-edged sword.
In single-threaded mode, everything is so monotonous and stable, all the resources are their own, and I am in charge of my resources.
In the multithreaded mode, multiple threads are loaded under a process, and each thread shares most of the system resources except that part of the resources are exclusive.
Process resources shared by multiple threads:
Memory
File descriptor
Address space
Global data
...
Resources that are exclusive to each thread:
Thread register
Thread stack
Thread ID, error return code, signal shielding code
...
Knock on the blackboard and highlight:
1. Process is the basic unit of resource allocation and scheduling in the system, and thread is the basic unit of CPU scheduling and dispatching.
two。 The process is the carrier of the thread, the process has an independent address space, and all threads share the address space of the process.
3. The process is the majority shareholder of the system resources, while the thread basically does not own the system resources and only takes up a small amount of resources that are essential to the operation, such as program counters, a set of registers and call stacks.
Multiple threads in the same process are a bit like sharing, we share most of the resources, own a small part of the resources, influence each other, but a single process and single thread is the whole rent, own all the resources, no one affects.
After mastering the characteristics of resource sharing and interaction in multithreading, it's much easier to look at thread safety and reentrant.
What is thread safety?
The so-called security in computers mostly refers to the correctness and predictability of the results.
As we know earlier, although multithreading can improve concurrency, multiple threads share a lot of resources, such as writing global data, in which case additional intervention is required, otherwise it will lead to messy results.
Thread safety is that in the process of parallel execution of multiple threads with shared data, it can be executed normally and correctly without unexpected conditions such as data contamination, otherwise it is called thread unsafe.
Generally speaking, thread-safe is not messy, and thread-safe is that it may be multifarious as soon as it runs.
Therefore, the root cause of thread unsafety may be: shared data and variable shared data.
These shared data include global variables, local static variables, etc., each thread may operate on this data, and the result of the operation will affect other threads.
We often mention another term: thread-safe functions / thread-safe classes.
Some features of thread-safe functions:
Without any shared data, it's all local data.
There is write sharing data, but it is locked, and the synchronous call of multi-thread can be realized.
There is read but no write shared data, no lock is required
You can see from the figure:
There are four worker threads in the same process
The common function An only performs the print operation, and whenever any thread calls it, the result is certain and correct, so it is a thread-safe function.
The common function B uses the global variable Count and increments it by 1, but there is no locking synchronization, so the result is uncertain and is a thread-unsafe function.
The common function C uses the global variable Factor, increments it by 2, and uses mutexes to synchronize to ensure that the results are correct. It is a thread-safe function.
When writing a multithreaded program, if multiple threads are involved in operating a common function, if the function itself is not thread-safe.
For example, when a function F is a thread-safe function, but F calls the thread-safe function G, G also needs to be locked, otherwise function F will not be safe.
The classification of thread unsafe functions is pointed out in depth in the book "in-depth understanding of computer Systems":
A function that does not protect shared output
Maintain a function that spans multiple call states
A function that returns a pointer to a static variable
Functions that call thread-unsafe functions
Most of the examples described above are related to the unlocked control of global variables, and there are two more:
Function this call depends on the result of the last call, that is, the so-called cross-state, the typical rand () function in Linux
Function to put the result in a global pointer, typical gethostbyname, localtime, strtok, etc.
/ / function prototype struct tm * localtime (const time_t * clock); / * localtime example * / # include # include int main () {time_t rawtime; struct tm * timeinfo; time (& rawtime); timeinfo = localtime (& rawtime); return 0;}
When the result is stored in timeinfo in localtime, this global variable can be manipulated by any thread, thus causing thread unsafety.
For thread-unsafe functions in Linux, you can check out:
Https://man7.org/linux/man-pages/man7/pthreads.7.html
Reentrant function
After understanding the relevant definitions and causes of thread safety, let's take a look at what is reentrant.
Let's first take a look at the relevant definitions of reentrant:
A program can be interrupted at any time, and then the system to execute another piece of code, and then call to continue the original subroutine without error, it is called reentrant (reentrant or re-entrant).
Fundamentally speaking:
The reentrant function only uses variables on its own stack and does not rely on any external data, allowing multiple copies of the function to run, because the function stacks generated by each caller are independent of each other.
The non-reentrant function uses some system resources, which may cause problems if it is interrupted.
Reentrant functions are divided into two categories:
Explicitly reentrant: all function arguments are passed in values, and only local stack variables are used, then the function is shown to be reentrant, no matter how called, is reentrant, is absolutely unconditional.
Implicit reentrant: some parameters in the reentrant function are passed by reference, and it is reentrant and relatively conditional only when a pointer to non-shared data is passed when the thread is called.
The reentrant function needs to meet the following conditions:
Static or global data is not used inside the function
The function does not return static or global data, and data generation is provided by the caller.
Do not call the non-reentrant function
In essence, the reentrant function realizes the separation of algorithm and data, and the internal calculation of the function does not depend on the external, does not affect and is not affected by the external influence, so it is an efficient and safe function.
Reentrant functions are thread-safe functions, and thread safety is not necessarily reentrant functions.
A non-reentrant function can be modified in accordance with the reentrant rule to become a reentrant function.
At this point, I believe you have a deeper understanding of "what are the characteristics of multithreading concurrency". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.