In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about how to avoid data competition in C++. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
Reason (reason)
Unless you do, nothing is guaranteed to work and subtle errors will persist.
Unless you do, nothing can guarantee the action, and subtle mistakes will continue.
Note (Note)
In a nutshell, if two threads can access the same object concurrently (without synchronization), and at least one is a writer (performing a non-const operation), you have a data race. For further information of how to use synchronization well to eliminate data races, please consult a good book about concurrency.
In short, if two threads can access the same object concurrently (without any synchronization), and at least one thread performs a write operation (performing a non-constant operation), data contention occurs. For further information on how to better use synchronization to eliminate data competition, check out the classic books on concurrency.
Example, bad (negative example)
There are many examples of data races that exist, some of which are running in production software at this very moment. One very simple example:
There are many examples of data competition, some of which occur in running production-level software. Here is a simple example:
Int get_id ()
{
Static int id = 1
Return id++
}
The increment here is an example of a data race. This can go wrong in many ways, including:
The incremental operation in the code is an example of data competition. There are many ways to make mistakes, including:
Thread A loads the value of id, the OS context switches An out for some period, during which other threads create hundreds of IDs. Thread An is then allowed to run again, and id is written back to that location as A's read of id plus one.
Thread A takes the value of id and the operating system context exits from A for a period of time, when hundreds of ID are generated by other threads. Thread A then continues to run, and the id is rewritten, and the value is the result of adding 1 to the local variable read by A.
Thread An and B load id and increment it simultaneously. They both get the same ID.
Threads An and B get the id and add 1 at the same time. They get the same ID.
Local static variables are a common source of data races.
Local static variables are a common source of data competition.
Example, bad (negative example):
Void f (fstream& fs, regex pattern)
{
Array buf
Int sz = read_vec (fs, buf, max); / / read from fs into buf
Gsl::span s {buf}
/ /...
Auto H2 = async ([&] {sort (std::execution::par, s);}); / / spawn a task to sort
/ /...
Auto h3 = async ([&] {return find_all (buf, sz, pattern);}); / / spawn a task to find matches
/ /...
}
Here, we have a (nasty) data race on the elements of buf (sort will both read and write). All data races are nasty. Here, we managed to get a data race on data on the stack. Not all data races are as easy to spot as this one.
Here, there is (severe) data contention for elements stored in buf (sorting includes both read and write operations). No data competition is not serious. Data competition in the code occurs in the data in the stack. Not all data competition is as easy to detect as this example.
Example, bad (negative example):
/ / code not controlled by a lock
Unsigned val
If (val < 5) {
/ /... Other thread can changeval here...
Switch (val) {
Case 0: / /...
Case 1: / /...
Case 2: / /...
Case 3: / /...
Case 4: / /...
}
}
Now, a compiler that does not know that val can change will most likely implement that switch using a jump table with five entries. Then, a val outside the [0..4] range will cause a jump to an address that could be anywhere in the program, and execution would proceed there. Really, "all bets are off" if you get a data race. Actually, it can be worse still: by looking at the generated code you may be able to determine where the stray jump will go for a given value; this can be a security risk.
Now, the compiler doesn't know that val will be modified, so the result of the compilation is likely to be a jump table with five branches. Then once the value of val is out of range [0.. 4], it is possible to switch to anywhere in the program and continue execution from there. Really, once there is a data competition, no one knows what the result will be. In fact, it could be worse: by examining the generated code, you may be able to figure out exactly where the program can jump to for a given value. This could be a security risk.
Enforcement (implementation recommendations)
Some is possible, do at least something. There are commercial and open-source tools that try to address this problem, but be aware that solutions have costs and blind spots. Static tools often have many false positives and run-time tools often have a significant cost. We hope for better tools. Using multiple tools can catch more problems than a single one.
Some are possible, at least do something. Some commercial and open source tools try to locate these problems, but it is important to note that the solution requires cost and there are blind spots. Static tools can generate a lot of false positives, while run-time tools are often costly. We hope that better tools will emerge. Using multiple tools will catch more errors than a single tool.
There are other ways you can mitigate the chance of data races:
There are other ways to reduce the likelihood of data competition:
Avoid global data
Avoid global data
Avoid static variables
Avoid static data
More use of value types on the stack (and don't pass pointers around too much)
Use value types more on the stack (and don't pass pointers back and forth)
More use of immutable data (literals, constexpr, and const)
Greater use of immutable data (literals, constexpr, and const)
The above is the editor for you to share in C++ how to avoid data competition, if there happen to be similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
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.