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--
C++ how to use the volatile keyword to achieve synchronization processing, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, there are people who need this can learn, I hope you can gain something.
Reason
In C++, unlike some other languages, volatile does not provide atomicity, does not synchronize between threads, and does not prevent instruction reordering (neither compiler nor hardware). It simply has nothing to do with concurrency.
Unlike other languages, volatile in C++ does not guarantee atomicity, does not synchronize between threads, and does not prevent instruction rearrangement (either compiler or hardware). It doesn't do anything for concurrency.
Example, bad:
int free_slots = max_slots; // current source of memory for objects
Pool* use(){ if (int n = free_slots--) return &pool[n];}
Here we have a problem: This is perfectly good code in a single-threaded program, but have two threads execute this and there is a race condition on free_slots so that two threads might get the same value and free_slots. That's (obviously) a bad data race, so people trained in other languages may try to fix it like this:
There is a problem with the code: in a single-threaded program, this is perfect code, but it will be executed by two threads, and there will be data contention on free_slots, resulting in two threads getting the same value and free_slots. This is (obviously) a bad data race, so people trained in other languages might solve this problem like this:
volatile int free_slots = max_slots; // current source of memory for objects
Pool* use(){ if (int n = free_slots--) return &pool[n];}
This has no effect on synchronization: The data race is still there!
The C++ mechanism for this is atomic types:
This has no effect on synchronization: the data race is still on! C++ implementation of data synchronization mechanism atomic type:
atomic free_slots = max_slots; // current source of memory for objects
Pool* use(){ if (int n = free_slots--) return &pool[n];}
Now the -- operation is atomic, rather than a read-increment-write sequence where another thread might get in-between the individual operations.
Now--the operation is atomic, not another thread can insert a read-increment-write sequence of operations.
Alternative
Use atomic types where you might have used volatile in some other language. Use a mutex for more complicated examples.
If you've ever used the volatile keyword in another language, use the atomic type. For more complex examples, mutex can be used.
See also
(rare) proper uses of volatile
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#cp200-use-volatile-only-to-talk-to-non-c-memory)
Did reading the above help you? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.
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.