# Mutex

`import { Mutex } from "destack:sync";`

Mutual exclusion lock.

```ds title="Mutex"
export class Mutex<T> {
    state: Atomic<uint32>;
    value: T;
    constructor(value: T);
    lock(): MutexGuard<T>;
    tryLock(): MutexGuard<T> | undefined;
    getExclusive(): &exclusive T;
    intoInner(): T;
}
```

[language/library/src/sync/mutex.ds:8:39](https://github.com/destack-sh/destack/blob/main/language/library/src/sync/mutex.ds#L8-L39)

## Members

### `state: Atomic<uint32>`

The lock state.

### `value: T`

The protected value.

### `constructor(value: T)`

Create a mutex protecting `value`.

### `lock(): MutexGuard<T>`

Lock this mutex, blocking the current worker.

### `tryLock(): MutexGuard<T> | undefined`

Try to lock this mutex without blocking.

### `getExclusive(): &exclusive T`

Borrow the protected value without locking.

### `intoInner(): T`

Consume this mutex and return the protected value.
