What this C++ memory-ordering interview question tests
This is an easy concurrency question that probes your understanding of how threads interact with shared memory in C++. It requires you to reason carefully about memory ordering semantics and what guarantees different synchronization primitives provide—or fail to provide—across thread boundaries.
The question asks you to trace the execution of a small multi-threaded program and predict the value observed by one thread when reading a shared variable. To solve it, you need to understand the difference between relaxed, acquire/release, and sequential consistency ordering in C++'s atomic library, and recognize when synchronization is truly guaranteed versus when a race condition or visibility issue can occur. This skill is essential for writing correct concurrent code in performance-critical systems.
- Relaxed vs. acquire/release vs. sequential consistency atomics
- Memory barriers and synchronization points
- Data races and undefined behavior in concurrent contexts
- Visibility and ordering of writes across threads