ST
StateTrace
Visual Quant & Low-Latency Systems Lab
GitHub
Concepts/Synchronization/Shared Counter

Shared Counter

Trace how different synchronization approaches affect correctness and latency. Step through the same workload three ways and see where shared state goes wrong.

Steps
1/6
Stateshared + per-thread
T1line 4
T1 localread
local0
shared
shared memory
counter0
counter per stepnow = 0
T2
T2 local
local
Per increment2.50nsmedian of 10 reps
Correctness 74% lost1,026,290 / 4,000,000
vs Non-Atomicbaseline (but wrong)fast but breaks
Setup4 threadsCPP · 1M iters · Apple M1 Pro
OBSERVED

T1 reads counter = 0 into its local variable.

Non-Atomic
T1
1int counter = 0;
2
3void worker() {
4    int local = counter;  // read
5    local = local + 1;    // compute
6    counter = local;      // write
7}
Historystep 1 / 6
step 1/6
Counter ended at 1,026,290 of 4,000,000 expected — 74% of writes vanished. The fix is one of these →