What this C++17 memory ordering interview question tests
This is a medium-difficulty concurrency question that probes your understanding of memory ordering semantics in C++17 and how different ordering constraints affect the visibility of atomic operations across threads. Intel and other firms that build systems with strict latency or correctness requirements use questions like this to verify that candidates can reason about memory model subtleties rather than treating atomics as a black box.
To solve it, you need to trace through the provided code snippet, identify the scenario where an unexpected behavior or race condition occurs, and then evaluate which memory orderings would or would not prevent that failure. The key is understanding the guarantees (and limits) of each ordering level—from relaxed through release-acquire to sequential consistency—and how they interact with loads, stores, and synchronization points.
- Memory ordering guarantees: relaxed, acquire, release, acq_rel, seq_cst
- Synchronization and visibility between threads
- Happens-before relationships in the C++ memory model
- False positives: orderings that prevent the failure vs. those that do not