Atomic (AT05)
1: import { parentPort } from 'worker_threads';
2:
3: // Listen for messages from the main thread
4: parentPort.on('message', (event) => {
5: const sab = event.sab; // Extract the SharedArrayBuffer from the message
6: const i32 = new Int32Array(sab);
7:
8: console.log(`Worker started and waiting...`);
9:
10: // Wait for the main thread to notify
11: Atomics.wait(i32, 0, 0); // Blocks until notified
12: console.log(`Worker awakened with value: ${i32[0]}`);
13: });
Worker thread to Atomics.notify()
Atomic context:
ES6 context:
Comments (
)

Link to this page:
http://www.vb-net.com/JavascriptES6/AT05.htm
|