In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to use the idiomatic pattern of C++". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought. Let's study and learn "how C++ uses idiomatic patterns".
CP.111: if you really need a double-checked lock, use idiomatic mode
Reason (reason)
Double check locks tend to mess things up. If you really need to use double-checked locks, regardless of C++ core guidelines CP.100: do not use lock-free programming, unless absolutely necessary and C++ core guidelines CP.110: do not write double-checked locking code for initialization, then follow the idiomatic pattern when using double-checked locks.
When a non-thread-safe action is difficult to occur and there are quick thread-safety tests to ensure that it is not needed, but there is no guarantee to the contrary, a double-checked lock mode that does not violate the C++ core guideline CP.110: do not write double-checked locking code for initialization.
Example, bad (negative example)
The use of volatile does not make the first check thread-safe, see also CP.200: Use volatile only to talk to non-C++ memory
The use of volatile does not allow the first to check thread safety, see CP.200: use volatile only when talking about non-C++ memory
Mutex action_mutex
Volatile bool action_needed
If (action_needed) {
Std::lock_guard lock (action_mutex)
If (action_needed) {
Take_action ()
Action_needed = false
}
} Example, good (sample) mutex action_mutex
Atomic action_needed
If (action_needed) {
Std::lock_guard lock (action_mutex)
If (action_needed) {
Take_action ()
Action_needed = false
}
}
Fine-tuned memory order may be beneficial where acquire load is more efficient than sequentially-consistent load
When sequential execution loads are more efficient than demand loads, it may be more advantageous to adjust a good memory order
Mutex action_mutex
Atomic action_needed
If (action_needed.load (memory_order_acquire)) {
Lock_guard lock (action_mutex)
If (action_needed.load (memory_order_relaxed)) {
Take_action ()
Action_needed.store (false, memory_order_release)
}
}
Thank you for your reading. the above is the content of "how to use idiomatic patterns on C++". After the study of this article, I believe you have a deeper understanding of how C++ uses idiomatic patterns. Specific use also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.