-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
32 lines (27 loc) · 800 Bytes
/
mod.ts
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
const workers: Worker[] = [];
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: unknown[]) {
console.log(`main:`, ...args);
}
for (let i = 0; i < count; ++i) {
const worker = new Worker(new URL("./worker.ts", import.meta.url), {
type: "module",
});
worker.addEventListener("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]);
}