# @esfx/threading-mutex

> Provides 'Mutex', a thread synchronization primitive for use with Workers

Latest version **1.0.0** (published 2022-10-20) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @esfx/threading-mutex
pnpm add @esfx/threading-mutex
yarn add @esfx/threading-mutex
bun add @esfx/threading-mutex
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2022-10-20 |
| First published | 2019-06-01 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 56.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 234 |
| Author | Ron Buckton |
| Maintainers | rbuckton |

## Links

- npm: https://www.npmjs.com/package/@esfx/threading-mutex
- Repository: https://github.com/esfx/esfx
- Homepage: https://github.com/esfx/esfx#readme
- Issues: https://github.com/esfx/esfx/issues
- npm.io page: https://npm.io/package/@esfx/threading-mutex

## Dependencies (2)

- [@esfx/disposable](https://npm.io/package/@esfx/disposable.md) ^1.0.0
- [@esfx/threading-lockable](https://npm.io/package/@esfx/threading-lockable.md) ^1.0.0

## Recent versions

- 1.0.0 (latest) — 2022-10-20
- 1.1.0-alpha.202307260322 (next) — 2023-07-26
- 1.0.0-dev.8 (dev) — 2022-10-20
- 1.1.0-alpha.202306080253 — 2023-06-08
- 1.1.0-alpha.202306061406 — 2023-06-06
- 1.1.0-alpha.202306061316 — 2023-06-06
- 1.1.0-alpha.202306051228 — 2023-06-05
- 1.1.0-alpha.202305082104 — 2023-05-08
- 1.1.0-alpha.202302091715 — 2023-02-09
- 1.1.0-alpha.202302071935 — 2023-02-07
- 1.1.0-alpha.202211291446 — 2022-11-29
- 1.1.0-alpha.202211221509 — 2022-11-22
- 1.1.0-alpha.202210252139 — 2022-10-25
- 1.0.0-dev.7 — 2022-10-20
- 1.0.0-dev.6 — 2022-10-19
- … 33 more at https://npm.io/package/@esfx/threading-mutex/versions

## README

# `@esfx/threading-mutex`

A `Mutex` class that performs locking across `Worker` instances using a `SharedArrayBuffer`.

# Overview

* [Installation](#installation)
* [Usage](#usage)
* [API](#api)

# Installation

```sh
npm i @esfx/threading-mutex
```

# Usage

```ts
import { Worker, isMainThread, parentPort, workerData } from "worker_threads";
import { Mutex } from "@esfx/threading-mutex";
import { StructType, int32 } from "@esfx/struct-type";
import { sleep } from "@esfx/threading-sleep";

const SharedData = StructType([
    { name: "x", type: int32 },
    { name: "y", type: int32 },
]);

function worker_thread() {
    const m = new Mutex(workerData[0]);
    const data = new SharedData(workerData[1]);

    while (true) {
        sleep(250);

        m.lock();
        try {
            // inside of the lock we can mutate 'data' without
            // main() seeing a partial update.
            data.x++;
            data.y--;
        }
        finally {
            m.unlock();
        }
    }
}

function main() {
    const m = new Mutex();
    const data = new SharedData(/*shared*/ true);
    const worker = new Worker(__filename, { workerData: [m.buffer, data.buffer] });

    while (true) {
        sleep(500);

        m.lock();
        try {
            // inside of the lock, we know that we can safely read
            // both 'x' and 'y' and the worker will not modify
            // either value until we unlock.
            console.log(`x: ${data.x}, y: ${data.y}`);
        }
        finally {
            m.unlock();
        }
    }
}

if (isMainThread) {
    main();
}
else {
    worker_thread();
}
```

# API

You can read more about the API [here](https://esfx.js.org/esfx/api/threading-mutex.html).

---
_Source: https://npm.io/package/@esfx/threading-mutex · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
