Skip to main content
Module

x/async/mod.ts>Mutex

🦕 Asynchronous primitive modules loosely port from Python's asyncio for Deno.
Go to Latest
class Mutex
Re-export
import { Mutex } from "https://deno.land/x/async@v2.0.0/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();
}

Properties

readonly
locked: boolean

Returns true if the mutex is locked, false otherwise.

Methods

acquire(): Promise<void>

Acquire the mutex, waiting if necessary for it to become available.

release(): void

Release the mutex, allowing the next pending acquirer to proceed.