Card 09/ 09
All 9 cards
RecapDifficulty: Advanced1 min
Two Failures, and What Fixes Which
Five things this topic established, and where each one was.
| The thing | Where |
|---|---|
| A race is correctness that depended on timing all along, and it usually passes | Two Threads Incrementing One Counter, Ten Million Times Each |
count++ is read, add, write — and another thread can get between any two | Why count++ Is Three Steps |
| Read-compute-write is the same shape whether it is three characters or four lines | The Same Failure Moving Money |
| A visibility problem is a write that happened and cannot be seen | The Other Failure |
volatile for a flag, an atomic for one value, a lock for several fields | volatile, synchronized and an Atomic Class |
The one worth carrying: there are two failures here and one of them is invisible. Before reaching for a tool, decide whether the problem is that two threads interleaved or that one of them cannot see what the other wrote — the fixes do not overlap.