# atomic-lock

> Node.js module for atomic locking

Latest version **1.0.1** (published 2022-03-12) · 0 weekly downloads

## Install

```sh
npm install atomic-lock
pnpm add atomic-lock
yarn add atomic-lock
bun add atomic-lock
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2022-03-12 |
| First published | 2022-03-12 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 2.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Edward |
| Maintainers | featurerich1 |
| Keywords | lock, mutex, atomics, compareExchange, parallelism |

## Links

- npm: https://www.npmjs.com/package/atomic-lock
- Homepage: https://github.com/featurerich1/atomic-lock
- npm.io page: https://npm.io/package/atomic-lock

## Alternatives

- [@oh-my-pi/pi-natives](https://npm.io/package/@oh-my-pi/pi-natives.md) — 51.8K weekly downloads
- [@capgo/capacitor-light-sensor](https://npm.io/package/@capgo/capacitor-light-sensor.md) — 3.0K weekly downloads
- [@heyhuynhgiabuu/pi-diff](https://npm.io/package/@heyhuynhgiabuu/pi-diff.md) — 492 weekly downloads
- [@lotsa/verdant-lang-asm](https://npm.io/package/@lotsa/verdant-lang-asm.md) — 38 weekly downloads
- [new-era-syntax](https://npm.io/package/new-era-syntax.md) — 20 weekly downloads

## Recent versions

- 1.0.1 (latest) — 2022-03-12
- 1.0.0 — 2022-03-12

## README

# atomic-lock

## Installation
```
npm install atomic-lock
```

## Description
1. Create a one-element int32 SharedArrayBuffer for the lock to use
2. Share that SharedArrayBuffer via workerData with your nodejs worker threads
3. Create atomic-lock instances with that SharedArrayBuffer in each worker thread
4. Lock your critical section
5. Unlock when finished

- https://github.com/featurerich1/atomic-lock
- This is a JavaScript rendition of the v3 Mutex from "Futexes Are Tricky" by Ulrich Drepper https://akkadia.org/drepper/futex.pdf

## Example
```js
const { Worker, workerData } = require("worker_threads")
const numThreads = 8
const sab = new Int32Array(new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT))
const result = new Int32Array(
  new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT)
)
let doneCount = 0
for (let i = 0; i < numThreads; i++) {
  const worker = new Worker(
    `
        const { workerData } = require('worker_threads')
        const { sab, result } = workerData
        const Lock = require('atomic-lock')
        const lock = new Lock(sab)
        for(let i = 0; i < 1_000_000; i++) {
            lock.lock()
            result[0] += 1
            lock.unlock()
        }
        `,
    { eval: true, workerData: { sab, result } }
  )
  worker.on("exit", (code) => {
    doneCount++
    if (doneCount === numThreads) {
      console.log(result) // 8,000,000
    }
  })
}
```

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