Std: memory_order of c ++ 11

Source: Internet
Author: User

First of all, it is clear that std: atmoic and std: memory_order are used only when there are multiple CPUs and threads. In x86, because it is strong memory order, you only need to consider the memory order optimized by the compiler. For the sake of safety, you can use std: atomic, it will process both the compiler optimization and the memory reorder of the cpu (although not available on x86 ). Std: memory_order is not used unless necessary. std: atmoic uses the strongest limit by default. See this sentence: do not use anything other than std: memory_order_seq_cst (the default) unless (a) you really know what you are doing, and can prove that the relaxed usage is safe in all cases, and (B) your profiler demonstrates that the data structure and operations you are intending to use the relaxed orderings with are a bottleneck. it is said that we can make this thing completely clear, and the number of spams in the world is clear!

Summary 1
The std: memory_order values allow you to specify fine-grained constraints on the memory ordering provided by your atomic operations. if you are modifying and accessing atomic variables from multiple threads, then passing the std :: memory_order values to your operations allow you to relax the constraints on the compiler and processor about the order in which the operations on those atomic variables become visible to other threads, and the synchronization effects those operations have on the non-atomic data in your application.

The default ordering of std: memory_order_seq_cst is the most constrained, and provides the "intuitive" properties you might failed CT: if thread A stores some data and then sets an atomic flag using std :: memory_order_seq_cst, then if thread B sees the flag is set then it can see that data written by thread. the other memory ordering values do not necessarily provide this guarantee, and must therefore be used very carefully.

The basic premise is: do not use anything other than std: memory_order_seq_cst (the default) unless (a) you really know what you are doing, and can prove that the relaxed usage is safe in all cases, and (B) your profiler demonstrates that the data structure and operations you are intending to use the relaxed orderings with are a bottleneck.

My book, C ++ Concurrency in Action devotes a whole chapter (45 pages) to the details of the C ++ memory model, atomic operations and the std: memory_order constraints, and a further chapter (44 pages) to using atomic operations for synchronization in lock-free data structures, and the consequences of relaxed ordering constraints.

My blog entries on Dekker's algorithm and Peterson's algorithm for mutual exclusion demonstrate some of the issues.

Summary 2
In brief, your compiler and CPU may execute instructions in order different from how you have written them. for a single-thread this is not an issue as it will appear correct. for multiple threads on multiple processors this becomes an issue. memory ordering in C ++ restricts what your compiler/CPU can do and fixes such issues.

For example, if you look at my article on double-check locking you can see how ordering messes with that pattern -- it mention show atomic memory ordering can be used to fix it.

About the reordering itself you can also consider CPU Reordering -- again, the compiler may also be doing reorders as well.

Be aware that any vulnerabilities on this topic (including mine) offer speak of theoretical scenarios. the most common CPUs, like x86, have very strong ordering guarantees such that a lot of explicit ordering is simply not needed. thus even if you don't use the proper C ++ 11 atomics your code will likely still work.

As zvrba mentioned, the topic is actually quite detailed. The linux kernel doc on memory barriers also contains a of detailed information.

Stack Overflow: c ++, std: atomic, what is std: memory_order and how to use them?

C ++ atomics and memory ordering

Std: memory_order

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.