Multicore & Synchronization

Why do C11 _Atomic and C++11 std::atomic let programmers specify a memory order?

  1. ATo specify where in memory the atomic variable is placed
  2. BChoosing only the ordering guarantee needed avoids unnecessary barriers and preserves performance
  3. 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 chooses and emits the 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.