@esfx/threading-spinwait
Provides SpinWait
, a thread synchronization primitive for use with Workers.
Overview
Installation
npm i @esfx/threading-spinwait
Usage
import { Worker, isMainThread, parentPort, workerData } from "worker_threads";
import { SpinWait } from "@esfx/threading-spinwait";
import { StructType, int32 } from "@esfx/struct-type";
const SharedData = StructType([
{ name: "ready", type: int32 },
]);
function worker_thread() {
const data = new SharedData(workerData);
data.ready = 1;
}
function main() {
const data = new SharedData( true);
const worker = new Worker(__filename, { workerData: data.buffer });
const spinWait = new SpinWait();
spinWait.spinUntil(() => data.ready === 1);
}
API
You can read more about the API here.