Race Condition

Watch how concurrent threads can corrupt shared data when operations aren't atomic. Compare unsafe vs atomic operations side by side.

🏎️ What is a Race Condition?

A race condition occurs when two or more threads access shared data simultaneously, and at least one of them modifies it. The final result depends on the unpredictable timing of thread execution — whoever "wins the race" determines the outcome. This unpredictability leads to bugs that are extremely hard to reproduce and debug.

Example: Imagine two bank tellers trying to update the same account balance at the same time. If the balance is $100 and both read it, then both try to add $50, the balance might end up as $150 instead of $200!

💡 Why Should You Care?

Real-World Impact

  • • Lost database updates
  • • Corrupted file contents
  • • Incorrect financial calculations
  • • Inventory management errors

Common Occurrences

  • • Multi-threaded web servers
  • • Concurrent API requests
  • • Shared cache updates
  • • Counter/analytics tracking

⚖️ Understanding the Trade-offs

Why Race Conditions Happen

  • • Non-atomic read-modify-write operations
  • • Lack of synchronization primitives
  • • Check-then-act patterns without locks
  • • Premature optimization avoiding locks

How to Prevent Them

  • • Use atomic operations (like in this demo)
  • • Apply mutexes/locks for critical sections
  • • Use thread-safe data structures
  • • Design for immutability when possible

🎬 What to Watch in This Demo

Click "Run Both Demos" to see two scenarios side-by-side: the unsafe version where all threads read the same value before any writes occur (causing lost updates), and the atomic version where each operation is indivisible, ensuring every update is preserved.

Unsafe (Race Condition)

counter += 1
Counter Value
0
Expected: 3 | Actual: 0
T1
Thread 1idle
T2
Thread 2idle
T3
Thread 3idle
Click "Run Demo" to see race condition

Atomic (Safe)

atomicIncrement()
Counter Value
0
Expected: 3 | Actual: 0
T1
Thread 1idle
T2
Thread 2idle
T3
Thread 3idle
Click "Run Demo" to see atomic operations

What's Happening?

Why Unsafe Fails

The operation counter += 1 is actually 3 steps:

  1. Read current value
  2. Add 1 to it locally
  3. Write new value back

When threads interleave, they all read the same initial value (0), each computes 1, and each writes 1. Two updates are lost!

Why Atomic Works

Atomic operations are indivisible - the entire read-modify-write happens as one unit.

  • Hardware guarantees no interleaving
  • Each thread sees the latest value
  • No updates can be lost

The counter correctly reaches 3 because each increment builds on the previous result.