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 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.
Practice this topic Back to topic

More Multicore & Synchronization questions

  1. Which correctly compares coarse-grained locking (one lock for a whole large data structure) with fine-grained locking (separate locks for each part)?
  2. What property does a lock-free algorithm have?
  3. Which describes a critical section?
  4. Which describes barrier synchronization in parallel programs?
  5. When 64 cores continuously update one shared counter with atomic instructions, adding cores does not speed things up. What is the main reason?