-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path1-shared.js
More file actions
20 lines (18 loc) · 584 Bytes
/
Copy path1-shared.js
File metadata and controls
20 lines (18 loc) · 584 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
'use strict';
const threads = require('node:worker_threads');
const { Worker, isMainThread } = threads;
if (isMainThread) {
const buffer = new SharedArrayBuffer(10);
new Worker(__filename, { workerData: buffer });
new Worker(__filename, { workerData: buffer });
} else {
const { threadId, workerData } = threads;
const array = new Int8Array(workerData);
const value = threadId === 1 ? 1 : -1;
setInterval(() => {
for (let i = 0; i < 10; i++) {
array[i] += value;
}
console.dir([ threadId, array ]);
}, 100); // change to 10 to see race condition
}