Card 09/ 09
Recap: Why Shared State Breaks
Races, non-atomic steps, visibility, and which tool fixes which, with where each was.
Why shared state breaks comes down to the five ideas below, each listed with the card that covers it.
| The idea | Where |
|---|---|
| A race is correctness that depended on timing all along, and it usually passes tests | Race Condition Example: A Counter Incremented Ten Million Times |
count++ is read, add, write, and another thread can get between any two | Atomic Operations: Why count++ Is Three Steps |
| Read-compute-write is the same shape in three characters or four lines | Race Condition Example: Moving Money Between Two Accounts |
| A visibility problem is a write that happened and cannot be seen | Memory Visibility: What One Thread Can See of Another's Writes |
volatile for a flag, an atomic for one value, a lock for several fields | volatile vs synchronized vs Atomic Classes |
Before reaching for a tool, decide which failure you have: two threads interleaving, or one thread unable to see what the other wrote. The fixes do not overlap.
← → move through sections and cards