Multicore & Synchronization
Why do C11 _Atomic and C++11 std::atomic let programmers specify a memory order?
- ATo specify where in memory the atomic variable is placed
- BChoosing only the ordering guarantee needed avoids unnecessary barriers and preserves performance
- CTo fix the order in which threads run in advance
Show answer and explanation
Answer: B. Choosing only the ordering guarantee needed avoids unnecessary barriers and preserves performance
Atomic operations default to sequential consistency (memory_order_seq_cst), which is easiest to reason about but costs the most in barriers. A simple counter only needs atomicity, so relaxed suffices; a flag that hands off data needs only acquire and release. The compiler emits the minimum instructions the CPU's memory model requires for the chosen order.
- A. It does not specify placement.
- B. Correct. The default is the strongest, sequential consistency.
- C. It does not decide thread execution order.