Made with Loami

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

  1. Read: copy the shared value into a private register.
  2. Modify: add one to the copy.
  3. 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.

–01234567890–012345678900123456789001234567890LOCK–01234567890–01234567890
Drum42A register42B register42Tokennot used

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)WorkerStepCounter after
0.0Aruns to drum41
0.4Bruns to drum41
1.7Areads 4141
2.1Breads 4141
2.4Aadds 1 → 4241
2.8Badds 1 → 4241
3.0Awrites 4242
3.4Bwrites 42 (over 42)42
3.7Areturns home42
4.1Breturns home42

Mutex

t (s)WorkerStepCounter after
0.0Aruns to gate41
0.4Bruns to gate41
1.0Atakes token, pin drops41
1.4Bwaits at gate41
1.6Aenters, docks41
2.3Areads 4141
3.0Aadds 1 → 4241
3.6Awrites 4242
4.3Abacks out42
5.0Areturns token42
5.6Areturns home42
5.6Btakes token, pin drops42
6.2Benters, docks42
6.9Breads 4242
7.6Badds 1 → 4342
8.2Bwrites 4343
8.9Bbacks out43
9.6Breturns token43
10.2Breturns home43

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

PartStands for
Carriage A, carriage BTwo threads, workers or async tasks
Register wheelsEach one's private copy: a CPU register or local variable. A carriage copies the drum into it (read)
Tally drumThe 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 keyThe 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 pinacquire(), which blocks until the token is free
Token back on the dockrelease()

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.