Card 09/ 09

RecapDifficulty: Advanced1 min

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.

What the topic established, and which card it was on
The ideaWhere
A race is correctness that depended on timing all along, and it usually passes testsRace Condition Example: A Counter Incremented Ten Million Times
count++ is read, add, write, and another thread can get between any twoAtomic Operations: Why count++ Is Three Steps
Read-compute-write is the same shape in three characters or four linesRace Condition Example: Moving Money Between Two Accounts
A visibility problem is a write that happened and cannot be seenMemory Visibility: What One Thread Can See of Another's Writes
volatile for a flag, an atomic for one value, a lock for several fieldsvolatile 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.