-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
41 lines (36 loc) · 957 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
33
34
35
36
37
38
39
40
41
const { lock, LockObj } = require("../dist/index");
const wait = (time) => new Promise(resolve => setTimeout(resolve, time));
const loop = (time, fn) => {
for(let i = 0; i < time; i++) fn();
};
let num = 0;
const delayIncrement = async () => {
const numcpy = num;
await wait(1);
num = numcpy + 1;
};
const locker = new LockObj();
const delayIncrementMutexed = async () => {
await lock(locker, async() => {
const numcpy = num;
await wait(1);
num = numcpy + 1;
})
}
(async () => {
console.log("Normal:");
loop(100, delayIncrement);
await wait(1);
loop(100, delayIncrement);
await wait(3000);
console.log("num is", num, "that was expected to be", 200);
console.log();
num = 0;
console.log("Mutexed:");
loop(100, delayIncrementMutexed);
await wait(1);
loop(100, delayIncrementMutexed);
await wait(3000);
console.log("num is", num, "that was expected to be", 200);
if(num !== 200) process.exit(1);
})();