@esfx/threading-semaphore Package

    Provides the Semaphore class, a thread synchronization primitive for use with Workers.

    Installation

    npm i @esfx/threading-semaphore
    

    Usage

    • TypeScript
    • JavaScript (CommonJS)
    import { Semaphore } from "@esfx/threading-semaphore";
    import { Worker, isMainThread, workerData } from "worker_threads";
    
    function worker_thread() {
        const sem = new Semaphore(workerData[0]);
    
        while (true) {
            // wait until the thread can enter the semaphore
            sem.wait();
    
            // do work inside the semaphore...
    
            // release this worker's spot
            sem.release();
    
            // do work outside the semaphore...
        }
    }
    
    function main() {
        // create a semaphore that allows 5 workers to enter at once
        const sem = new Semaphore(5);
    
        // start 10 workers
        for (let i = 0; i < 10; i++) {
            new Worker(__filename, { workerData: [sem.buffer] });
        }
    }
    
    if (isMainThread) {
        main();
    }
    else {
        worker_thread();
    }
    
    const { Semaphore } = require("@esfx/threading-semaphore");
    const { Worker, isMainThread, workerData } = require("worker_threads");
    
    function worker_thread() {
        const sem = new Semaphore(workerData[0]);
    
        while (true) {
            // wait until the thread can enter the semaphore
            sem.wait();
    
            // do work inside the semaphore...
    
            // release this worker's spot
            sem.release();
    
            // do work outside the semaphore...
        }
    }
    
    function main() {
        // create a semaphore that allows 5 workers to enter at once
        const sem = new Semaphore(5);
    
        // start 10 workers
        for (let i = 0; i < 10; i++) {
            new Worker(__filename, { workerData: [sem.buffer] });
        }
    }
    
    if (isMainThread) {
        main();
    }
    else {
        worker_thread();
    }
    

    Classes

    Semaphore

    • Improve this Doc
    Generated by DocFX