Card 09/ 09

RecapDifficulty: Advanced1 min

Two Failures, and What Fixes Which

Five things this topic established, and where each one was.

What the topic established, and which card it was on
The thingWhere
A race is correctness that depended on timing all along, and it usually passesTwo Threads Incrementing One Counter, Ten Million Times Each
count++ is read, add, write — and another thread can get between any twoWhy count++ Is Three Steps
Read-compute-write is the same shape whether it is three characters or four linesThe Same Failure Moving Money
A visibility problem is a write that happened and cannot be seenThe Other Failure
volatile for a flag, an atomic for one value, a lock for several fieldsvolatile, 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.