-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex1.js
More file actions
53 lines (42 loc) · 1.25 KB
/
ex1.js
File metadata and controls
53 lines (42 loc) · 1.25 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
'use strict';
/*
Problem:
Implement `incrementConcurrently(counter, times)`.
Input:
- counter: object like { value: number }
- times: number of concurrent increments
Goal:
- Run increments concurrently.
- Final value must increase by exactly `times`.
Constraint:
- There is an async gap in the increment path.
- You must prevent lost updates using a mutex abstraction.
Starter code is intentionally incorrect:
- Mutex does not serialize critical sections.
- Lost updates occur deterministically.
*/
class Mutex {
async runExclusive(fn) {
// Intentionally wrong: no queuing/serialization.
return fn();
}
}
async function incrementConcurrently(counter, times) {
if (!counter || typeof counter.value !== 'number') {
throw new TypeError('counter must be an object with numeric value');
}
if (!Number.isInteger(times) || times < 0) {
throw new TypeError('times must be an integer >= 0');
}
const mutex = new Mutex();
const tasks = Array.from({ length: times }, async () => {
await mutex.runExclusive(async () => {
const snapshot = counter.value;
await Promise.resolve();
counter.value = snapshot + 1;
});
});
await Promise.all(tasks);
return counter.value;
}
module.exports = { incrementConcurrently, Mutex };