Skip to content

Commit 1d9a67a

Browse files
committed
Handle leave event
1 parent 016cf06 commit 1d9a67a

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

JavaScript/9-web-locks.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ const locks = {
7676
if (kind === 'create') {
7777
const lock = new Mutex(resourceName, buffer);
7878
locks.resources.set(resourceName, lock);
79+
} else if (kind === 'leave') {
80+
for (const mutex of locks.resources) {
81+
if (mutex.trying) tryEnter();
82+
}
7983
}
8084
}
8185
};
@@ -106,17 +110,21 @@ if (isMainThread) {
106110

107111
new Thread();
108112
new Thread();
113+
setTimeout(() => {
114+
process.exit(0);
115+
}, 200);
109116

110117
} else {
111118

112119
locks.request('A', async lock => {
113-
console.log(`Exclusive A in ${threadId}`);
120+
console.log(`Enter A in ${threadId}`);
114121
});
115122

116-
setTimeout(() => {
117-
locks.request('B', async lock => {
118-
console.log(`Exclusive B in ${threadId}`);
123+
setTimeout(async () => {
124+
await locks.request('B', async lock => {
125+
console.log(`Enter B in ${threadId}`);
119126
});
127+
console.log(`Leave all in ${threadId}`);
120128
}, 100);
121129

122130
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy