1.0.0 • Published 2 years ago

@esfx/threading-semaphore v1.0.0

Weekly downloads
2
License
Apache-2.0
Repository
github
Last release
2 years ago

@esfx/threading-semaphore

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

Overview

Installation

npm i @esfx/threading-semaphore

Usage

import { Worker, isMainThread, parentPort, workerData } from "worker_threads";
import { Semaphore } from "@esfx/threading-semaphore";
import { StructType, int32 } from "@esfx/struct-type";
import { sleep } from "@esfx/threading-sleep";

const SharedData = StructType([
    { name: "running", type: int32 },
]);

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

API

You can read more about the API here.

1.0.0

3 years ago

1.0.0-dev.7

3 years ago

1.0.0-pre.42

3 years ago

1.0.0-dev.8

3 years ago

1.0.0-pre.41

3 years ago

1.0.0-dev.5

3 years ago

1.0.0-pre.44

3 years ago

1.0.0-dev.6

3 years ago

1.0.0-pre.43

3 years ago

1.0.0-dev.4

3 years ago

1.0.0-dev.0

3 years ago

1.0.0-pre.40

3 years ago

1.0.0-pre.31

3 years ago

1.0.0-pre.33

3 years ago

1.0.0-pre.35

3 years ago

1.0.0-pre.34

3 years ago

1.0.0-pre.37

3 years ago

1.0.0-pre.36

3 years ago

1.0.0-pre.39

3 years ago

1.0.0-pre.38

3 years ago

1.0.0-pre.30

4 years ago

1.0.0-pre.26

4 years ago

1.0.0-pre.25

4 years ago

1.0.0-pre.28

4 years ago

1.0.0-pre.29

4 years ago

1.0.0-pre.20

4 years ago

1.0.0-pre.19

4 years ago

1.0.0-pre.17

4 years ago

1.0.0-pre.16

5 years ago

1.0.0-pre.13

6 years ago

1.0.0-pre.12

6 years ago

1.0.0-pre.11

6 years ago

1.0.0-pre.9

6 years ago

1.0.0-pre.8

6 years ago