Skip to content
Know.itKnow.it
Create an animated lesson
On request

This article is free to read. Creating a lesson uses your daily or stored credits; review the language and settings before starting.

What is a race condition?

TechnologyLesson on requestFree to read

Quick answer

A race condition is a bug where a program's result depends on the unpredictable timing of two or more operations running at once. When tasks 'race' to read and change shared data, they can interfere and produce wrong or inconsistent results.

Key things to understand

  1. It happens when concurrent threads or processes touch the same data without coordination.
  2. Classic example: two transactions read a balance, both subtract, and one update is silently lost.
  3. The bugs are intermittent and hard to reproduce because they depend on exact timing.
  4. Fixed with locks, atomic operations, or other synchronization so only one task changes the data at a time.

People also ask

Why are race conditions so hard to debug?
They appear only under specific timing, so they may vanish when you add logging or attach a debugger, making them frustratingly intermittent.
How do you prevent a race condition?
Coordinate access to shared data with locks, mutexes, atomic operations, or transactions so changes can't overlap.
Are race conditions a security risk?
Yes — 'time-of-check to time-of-use' races can let an attacker slip a change in between a check and the action that relies on it.