https://github.com/mstephen19/tiny-mutex
Lightweight and simple mutexes in Node.js.
https://github.com/mstephen19/tiny-mutex
concurrency mutex node nodejs syncrhonization typescript
Last synced: about 1 month ago
JSON representation
Lightweight and simple mutexes in Node.js.
- Host: GitHub
- URL: https://github.com/mstephen19/tiny-mutex
- Owner: mstephen19
- Created: 2023-01-17T00:51:12.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-12-29T02:37:17.000Z (5 months ago)
- Last Synced: 2025-04-06T18:18:20.835Z (about 2 months ago)
- Topics: concurrency, mutex, node, nodejs, syncrhonization, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/tiny-mutex
- Size: 4.88 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Tiny Mutex
[](https://www.typescriptlang.org/) [](https://packagephobia.com/result?p=tiny-mutex@latest)
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](https://www.npmjs.com/package/piscina).
- Simple to use. Modeled after Golang's [`sync.Mutex`](https://go.dev/tour/concurrency/9)## Usage
```TypeScript
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);
}
```