Summary
Main thread vs Worker vs SharedArrayBuffer — who owns which buffer. Atomics for coordination. COOP/COEP required for SAB. Race conditions if no proper memory ordering. Prefer message-passing unless profiling proves copy cost too high.
Code example
const sab = new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT);
const view = new Int32Array(sab);
Atomics.store(view, 0, 0);
worker.postMessage({ sab });
// worker: Atomics.add(view, 0, 1); Atomics.notify(view, 0);
Key points
- Main thread vs Worker vs SharedArrayBuffer — who owns which buffer.
- Atomics for coordination.
- COOP/COEP required for SAB.
- Race conditions if no proper memory ordering.
- Prefer message-passing unless profiling proves copy cost too high.