Frontend Labs
⋯

public/js/workers/counter.js

// Increments a counter in shared memory n times (§39).
// atomic=false: load, add, store — three steps another thread can interleave with.
// atomic=true:  Atomics.add — one indivisible read-modify-write.
self.onmessage = ({ data: { sab, n, atomic } }) => {
  const counter = new Int32Array(sab);
  if (atomic) for (let i = 0; i < n; i++) Atomics.add(counter, 0, 1);
  else for (let i = 0; i < n; i++) counter[0]++;
  self.postMessage("done");
};

Generated from SOURCE.md · 8 Oct 2026 16:36