demo · v133

Real spinlock in two Workers

The TC39 proposal motivates Atomics.pause() with a thread spin-waiting on shared memory. This page runs the real shape: one producer Worker flips a SharedArrayBuffer flag on a timer while one consumer Worker spins on that flag, once tightly and once with Atomics.pause().

Shared memory isolation

checking

Atomics.pause in worker

checking

plain tight spin

empty flag probes
validated handoffs
checksum

spin with Atomics.pause()

empty flag probes
validated handoffs
checksum
Run the benchmark to compare wall time and how many times the consumer observed an empty flag.

the pattern

The producer waits for the shared flag to return to 0, waits on its own timer, writes the next value, then stores 1. The consumer runs on a separate Worker and spins until it sees 1. The only difference between the two measured runs is whether the consumer calls Atomics.pause() while the flag is still empty.

// Consumer Worker
while (Atomics.load(cells, FLAG) !== READY) {
  emptyChecks++;
  if (usePause) Atomics.pause(1 + (emptyChecks & 31));
}
const value = Atomics.load(cells, VALUE);
Atomics.store(cells, FLAG, EMPTY);
Atomics.notify(cells, FLAG, 1);

The proposal says the method is a spin-wait hint with no observable behavior other than timing, so this demo reports both elapsed time and the count of empty probes to make the wait visible.

see also