← Lessons

quiz vs the machine

Platinum1760

Concurrency

Data Race vs Race Condition

Two terms often confused: one is about memory, the other about outcomes.

5 min read · advanced · beat Platinum to climb

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.

Check yourself

Answer to earn rating on the learn ladder.

1. What precisely defines a data race?

2. Can a correctly synchronized program still have a race condition?