import { Mutex } from "https://deno.land/x/async@v2.0.2/mod.ts";
A mutex (mutual exclusion) is a synchronization primitive that grants exclusive access to a shared resource.
This is a low-level primitive. Use Lock
instead of Mutex
if you need to access a shared value
concurrently.
import { AsyncValue } from "./testutil.ts";
import { Mutex } from "./mutex.ts";
const count = new AsyncValue(0);
async function doSomething() {
const v = await count.get();
await count.set(v + 1);
}
// Critical section
const mu = new Mutex();
await mu.acquire();
try {
await doSomething();
} finally {
mu.release();
}