Memory Model and Data Races
CONCEPTS:C++ Memory Model and Data Races
Question Variations
- "What is a data race in C++ and why is it serious?"
- "When should you use a mutex versus `std::atomic`?"
- "What does acquire-release ordering provide?"
- "Does `volatile` make a shared variable thread-safe?"
Why This Is Asked
Concurrency bugs in C++ can be undefined behavior rather than merely inconsistent results. This question tests whether you can identify a data race, use synchronization to establish visibility, and avoid reaching for atomics when a mutex gives a clearer correct design.
Key Concepts
- A data race is conflicting unsynchronized access to the same non-atomic memory from multiple threads.
- A data race is undefined behavior in C++.
- Mutex lock/unlock operations establish synchronization between threads.
- Atomics support low-level synchronization with explicit memory-order trade-offs.
Question Variations
- “What is a data race in C++ and why is it serious?”
- “When should you use a mutex versus
std::atomic?” - “What does acquire-release ordering provide?”
- “Does
volatilemake a shared variable thread-safe?”