What is the difference between a race condition and a deadlock?

.NET Framework & CLR Concurrency Intermediate 2–3 Yrs3–5 Yrs Conceptual 1 min read

Quick Answer

A race condition is when the result depends on the unpredictable timing of threads — e.g. two threads both check a balance then both withdraw, and the total goes negative. A deadlock is when threads are permanently stuck waiting for each other's locks. A race produces wrong results; a deadlock produces a hang.

A race condition is a correctness bug — the outcome depends on which thread happens to run first. The textbook example is two threads reading an account balance, both seeing enough money, and both withdrawing, so it goes negative. A deadlock is a liveness bug — two threads are each holding a lock the other one needs, so neither can move and the app just hangs. So a race gives you a wrong answer intermittently; a deadlock gives you a freeze. You fix races with synchronisation or atomic operations, and deadlocks by always taking locks in the same order.

  • Race: the classic “check-then-act” or “read-modify-write” without synchronisation. Fix with a lock, an atomic operation, or a database-level guarantee.
  • Deadlock: two threads acquire locks A and B in opposite orders. Fix with consistent lock ordering, lock timeouts, or lock-free designs.
  • Related: livelock (threads keep reacting to each other and make no progress) and starvation (a thread never gets scheduled).