Skip to content

Race condition

Category: Hazards · Status: stub · Lessons: chapter 02, Shared state

One line: The result depends on the relative timing of tasks, and some timings give a wrong result — whether or not there is also a data race.

Also called: race, interference, general race, resource race.

How it connects

flowchart LR
  n_aba_problem["ABA problem"]
  n_data_race["Data race"]
  n_heisenbug["Heisenbug"]
  n_interleaving["Interleaving"]
  n_mutual_exclusion["Mutual exclusion"]
  n_race_condition["Race condition"]
  n_safety_failure["Safety failure"]
  n_toctou["Time of check to time of use"]
  n_aba_problem -->|is a| n_race_condition
  n_data_race ---|vs| n_race_condition
  n_interleaving -->|can cause| n_race_condition
  n_mutual_exclusion -->|prevents| n_race_condition
  n_race_condition -->|can cause| n_heisenbug
  n_race_condition -->|is a| n_safety_failure
  n_toctou -->|is a| n_race_condition
  classDef center stroke-width:3px
  class n_race_condition center
  classDef outside stroke-dasharray: 4 3
  class n_aba_problem,n_data_race,n_heisenbug,n_interleaving,n_mutual_exclusion,n_safety_failure,n_toctou outside

In each language

Rust The Rustonomicon ↗: Rust prevents data races but not general race conditions, which it calls impossible to prevent when you do not control the scheduler
Java ConcurrentHashMap: putIfAbsent, replace and computeIfAbsent do a check and an update as one atomic action

Where to read more