Not the same thing
People use data race and race condition interchangeably, but they are distinct, and a program can have one without the other.
Data race
A data race is a precise, language defined condition: two threads access the same memory location concurrently, at least one access is a write, and there is no synchronization ordering them. In many memory models a data race is undefined behavior. It is a property of memory accesses, detectable by tools.
Race condition
A race condition is a higher level flaw: the program's correctness depends on timing or interleaving of events. It can exist even when every memory access is properly synchronized, because the bug is in the logic, such as a check and act that is individually atomic but wrong as a sequence.
How they relate
- A data race is often, but not always, the cause of a race condition.
- You can fix a data race by adding atomics yet still have a race condition in your logic.
- You can have a race condition over external state, like files, with no shared memory at all.
Key idea
A data race is unsynchronized concurrent memory access with a write; a race condition is timing dependent incorrectness. Fixing one does not guarantee fixing the other.