Concepts/Synchronization/Race Condition
Race Condition
The same read–compute–write bug class in three domains. Bank balance loses a withdrawal. Lazy initialization leaks an allocation. Ticket booking double-books a seat. Same shape, different surface.
Steps
1/12
Stateshared + per-thread
T1
T1 local
statusidle
state—
decision—
customer_id—
result—
shared
shared memoryread
seats.12Cfree
confirmations0
counter per stepnow = 0
T2
T2 local
statusidle
state—
decision—
customer_id—
result—
Race conditions are a bug class. Correctness, not latency, is the measurement.
OBSERVED
seats[12C] = 'free'. Both worker threads are idle, waiting for a booking call.
Ticket Booking
1seats = {"12C": "free"}
2
3book_seat(seat_id, customer_id):
4 state := seats[seat_id] # read
5 if state == "free": # compute (decision)
6 seats[seat_id] := customer_id # write
7 return "confirmed"
8 return "unavailable"Historystep 1 / 12
space · ← → · rstep 1/12
Same race shape, different surface. Walk the other two for the full pattern.