Interview Question

What are data races?

Unsynchronized conflicting concurrent access creates a data race and undefined behavior.

💡 Concept ✅ Quick Revision 🧠 C++

Answer

A data race occurs when potentially concurrent conflicting actions access the same memory location without the required synchronization and neither is atomic. • Two actions conflict when one starts or ends an object lifetime or modifies the location and the other reads or modifies it. • A C++ program with a data race has undefined behavior. • Use mutexes, atomics, or thread confinement to establish safe ordering.

💡 C++ Example

std::mutex mutex; int value = 0; void increment() { std::lock_guard<std::mutex> lock{mutex}; ++value; }

⚡ Quick Revision

Unsynchronized conflicting concurrent access creates a data race and undefined behavior.