Offload functions to worker threads with shared memory primitives for Node.js.
1// Shared memory between main thread and workers using int32atomic.
2// Requires Node v24+.
3//
4// Run: node examples/atomics/main.ts
5
6import { workers, int32atomic } from '../../src/index.ts';
7import { increment } from './increment.ts';
8
9const counter = int32atomic();
10
11{
12 using run = workers(4);
13
14 // Fire off 100 increments across 4 workers
15 await Promise.all(Array.from({ length: 100 }, () => run(increment(counter))));
16}
17
18console.log(`Expected: 100`);
19console.log(`Actual: ${counter.load()}`);