Skip to content

Stepwise 1.5 — race conditions, frame by frame

Latest

Choose a tag to compare

@nirbhayagga nirbhayagga released this 04 Sep 23:11

The Systems section grows a second module: race conditions you can scrub through, one instruction at a time.

Threads & Race Conditions (new module)

  • Simulated threads run the same tiny program against shared memory; a seeded scheduler decides which thread executes its next instruction. A fixed seed means a fixed interleaving — fully deterministic, so it plays, pauses and rewinds like every other Stepwise timeline. A Shuffle interleaving button rolls a new seed.
  • Counter Without a Lock — counter++ is really LOAD / INC / STORE, and the default interleaving is chosen so you watch two of four increments vanish: a thread stores a stale register over a newer value, and the shared counter ends at 2 instead of 4.
  • Counter With a Mutex — the same program with the read–modify–write in a critical section. Threads that arrive while the lock is held show up blocked; nothing is lost, at the price of making that code serial again.
  • Atomic Counter — the increment as one indivisible fetch-and-add: correct with no lock and no blocking.
  • Per-thread cards (iteration, private register, next instruction, ready/blocked/finished), the executed-instruction strip, and metrics for context switches and lost updates. Threads (2–4), increments and seed are all editable.

Amdahl's law

An interactive panel under the visualizer: drag the parallel-fraction slider and see the speedup curve over 1–64 cores flatten against its 1/(1−p) ceiling (dashed), with Gustafson's scaled-workload answer as an optional overlay — and a reminder that the serial part is exactly the critical sections above.

Verification

9 new tests, 143 passing in total. Mutex and atomic variants must land exactly on the expected count across 25 seeds while the racy one loses updates on most of them; mutual exclusion is checked in every frame of 4-thread runs (never two threads past acquire, and the one inside always matches the lock holder); interleavings are reproducible per seed; and the shipped default is required to demonstrate the race, so it can never silently become a boring example.