tiny-mutex
TypeScript icon, indicating that this package has built-in type declarations

0.0.2 • Public • Published

Tiny Mutex

TypeScript Install size

Lightweight and simple mutexes.

Features

  • Zero dependencies. Small bundle size.
  • Written in TypeScript.
  • Simple to use. Modeled after Golang's sync.Mutex
  • Modern ESModules support.
  • Thread-safe. Works perfectly with multithreading libraries like Nanolith.

Usage

It's dead simple. There are only three functions to know.

import { newMutex, lock, unlock } from 'tiny-mutex'

// Create a mutex
const mutex = newMutex();

async function doWorkflow(shared: Uint8Array) {
    // Wait until the mutex is unlocked, then lock it.
    await lock(mutex);

    // Modify a resource normally without worrying about
    // other threads modifying it at the same time.
    shared.set([1, 2, 3])

    // Unlock the mutex once finished with modifying the
    // resource, unblocking other agents.
    unlock(mutex);
}

Package Sidebar

Install

npm i tiny-mutex

Weekly Downloads

0

Version

0.0.2

License

MIT

Unpacked Size

3.77 kB

Total Files

4

Last publish

Collaborators

  • mstephen19