-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
32 lines (26 loc) · 783 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { Worker } from "node:worker_threads";
const workers = [];
let doneCount = 0;
const count = 12;
const sharedArrayBuffer = new SharedArrayBuffer(1);
const mutexStore = new SharedArrayBuffer(4);
const typedArray = new Uint8Array(sharedArrayBuffer);
typedArray[0] = 10;
function log(...args) {
console.log(`main:`, ...args);
}
for (let i = 0; i < count; ++i) {
const worker = new Worker(new URL("./worker.js", import.meta.url));
worker.on('message', () => {
doneCount = doneCount + 1;
if (doneCount === count) {
queueMicrotask(() => terminate());
}
});
workers.push(worker);
worker.postMessage({ sharedArrayBuffer, mutexStore });
}
function terminate() {
workers.forEach(worker => worker.terminate());
log(`final value`, typedArray[0]);
}