Thread-Safe Synchronization with Atomics
Atomics provides static methods for performing atomic operations on SharedArrayBuffer data. This is crucial for multi-threaded JavaScript (Web Workers) to prevent race conditions and synchronize memory access between threads.
const buffer = new SharedArrayBuffer(1024);const i32 = new Int32Array(buffer);// In Worker AAtomics.store(i32, 0, 123);Atomics.notify(i32, 0, 1);// In Worker Bconst result = Atomics.wait(i32, 0, 0);if (result === 'ok') {console.log(Atomics.load(i32, 0));}