worker-rwlock
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

worker-rwlock

Usage

const {
  acquireReadLock,
  releaseReadLock,
  acquireWriteLock,
  releaseWriteLock,
  acquireReadLockAsync,
  acquireWriteLockAsync
} = require("worker-rwlock")
// or
import {
  acquireReadLock,
  releaseReadLock,
  acquireWriteLock,
  releaseWriteLock,
  acquireReadLockAsync,
  acquireWriteLockAsync
} from "worker-rwlock"

// Must be at least 12 bytes long
const lock = Int32Array(SharedArrayBuffer(12))

// Multiple threads can hold read locks at one time
// Note: This cannot enforce the thread to only read the data

acquireReadLock(lock)
// Read the data
releaseReadLock(lock)

// Write locks are exclusive: if a thread has a write lock then no other
// thread can access the data at all

acquireWriteLock(lock)
// Write the data
releaseWriteLock(lock)

// Async versions are also available for V8 only

await acquireReadLockAsync(lock)
// Read the data
releaseReadLock(lock)

await acquireWriteLockAsync(lock)
// Write the data
releaseWriteLock(lock)

// More information: https://en.wikipedia.org/wiki/Readers%E2%80%93writer_lock

Package Sidebar

Install

npm i worker-rwlock

Weekly Downloads

0

Version

0.1.0

License

MIT

Unpacked Size

5.43 kB

Total Files

5

Last publish

Collaborators

  • endershadow8