Skip to content

mstephen19/tiny-mutex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tiny Mutex

TypeScript Install size

Lightweight and simple mutexes.

Features

  • Zero dependencies. Small bundle size.
  • Written in TypeScript.
  • CommonJS & ESModules support.
  • Thread-safe. Works with worker threads and multithreading libraries like Piscina.
  • Simple to use. Modeled after Golang's sync.Mutex

Usage

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

// Create a mutex, usable in a cross-thread context
const mutex = createMutex();

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);
}

Releases

No releases published

Packages

No packages published