Why read-modify-write needs a mutex
Two workers each add one to a shared counter. Without a lock both can read the same value, and one increment disappears.
Read, add, write: three steps that can interleave
- Read: copy the shared value into a private register.
- Modify: add one to the copy.
- Write: store the copy back over the shared value.
Without a lock, if B reads before A's write lands, both copies hold 41 and both write 42: one +1 is lost. With the mutex, B waits at its gate for the single token until A has written, then reads 42.
With B starting 0.4 s after A: no lock ends at 42, one +1 lost; mutex ends at 43.
Race window in this model: 2.0 s, from A's read until A's write lands. The timings are illustrative and slowed down; a real window depends on the code, hardware and scheduler.
Step-by-step schedule
Generated from the same model that moves the figure. The start gap follows the slider on the Figure tab.
No lock
| t (s) | Worker | Step | Counter after |
|---|---|---|---|
| 0.0 | A | runs to drum | 41 |
| 0.4 | B | runs to drum | 41 |
| 1.7 | A | reads 41 | 41 |
| 2.1 | B | reads 41 | 41 |
| 2.4 | A | adds 1 → 42 | 41 |
| 2.8 | B | adds 1 → 42 | 41 |
| 3.0 | A | writes 42 | 42 |
| 3.4 | B | writes 42 (over 42) | 42 |
| 3.7 | A | returns home | 42 |
| 4.1 | B | returns home | 42 |
Mutex
| t (s) | Worker | Step | Counter after |
|---|---|---|---|
| 0.0 | A | runs to gate | 41 |
| 0.4 | B | runs to gate | 41 |
| 1.0 | A | takes token, pin drops | 41 |
| 1.4 | B | waits at gate | 41 |
| 1.6 | A | enters, docks | 41 |
| 2.3 | A | reads 41 | 41 |
| 3.0 | A | adds 1 → 42 | 41 |
| 3.6 | A | writes 42 | 42 |
| 4.3 | A | backs out | 42 |
| 5.0 | A | returns token | 42 |
| 5.6 | A | returns home | 42 |
| 5.6 | B | takes token, pin drops | 42 |
| 6.2 | B | enters, docks | 42 |
| 6.9 | B | reads 42 | 42 |
| 7.6 | B | adds 1 → 43 | 42 |
| 8.2 | B | writes 43 | 43 |
| 8.9 | B | backs out | 43 |
| 9.6 | B | returns token | 43 |
| 10.2 | B | returns home | 43 |
The same race in code
An unguarded shared increment
const r = count; // read
const next = r + 1; // modify
count = next; // write
Whether these steps can interleave depends on the language and on what is shared. When threads share plain memory (for example in C, Java or Go) a count++ is not guaranteed to be atomic. Single-threaded JavaScript can't be interrupted inside a synchronous count++; the race appears when an await separates the read from the write.
JavaScript: the race across an await
async function increment(store) {
const r = await store.get("count"); // read
await store.set("count", r + 1); // modify + write
}
// Both gets can finish before either set: +1, not +2.
await Promise.all([increment(store), increment(store)]);
Fix: hold a mutex across all three steps
const release = await mutex.acquire(); // wait for the token
try {
const r = await store.get("count");
await store.set("count", r + 1);
} finally {
release(); // token back on the dock
}
For one integer: an atomic read-modify-write
// Int32Array over a SharedArrayBuffer, shared between workers
Atomics.add(counts, 0, 1);
An atomic does one operation indivisibly. Use a lock when the read and the write are separate calls, or the section touches more than one value. Keep the section short: whoever waits is idle for all of it.
How the machine maps to the code
| Part | Stands for |
|---|---|
| Carriage A, carriage B | Two threads, workers or async tasks |
| Register wheels | Each one's private copy: a CPU register or local variable. A carriage copies the drum into it (read) |
| Tally drum | The shared value: memory, a database row, a cache key. A write rolls it to the carriage's number, so it keeps only the last write |
| +1 key | The modify step |
| Token on the tower (the one lit part) | The mutex: one exists, one holder at a time. It is unused when the lock is off |
| Gate pin | acquire(), which blocks until the token is free |
| Token back on the dock | release() |
Timings are illustrative, not measurements: each step is slowed to 0.6 to 1.0 s so it can be watched. A read copies the value the drum holds when the register seats; a write counts when it finishes, so the Drum value under the figure changes only then. Positions, wheels, the corner readout, the drum/register/token summary and both traces all come from one schedule model.
Figure made with the Anatomy skill and kit by Ryan (MIT License, © 2026 Ryan). Document runtime: Loami 0.2.0. Without JavaScript, the drawing shows one still: no lock, with B's write landing on top of A's.