1.0.0 • Published 2 years ago
@esfx/threading-conditionvariable v1.0.0
@esfx/threading-conditionvariable
Provides ConditionVariable
, a thread synchronization primitive for use with Workers.
Overview
Installation
npm i @esfx/threading-conditionvariable
Usage
import { Worker, isMainThread, parentPort, workerData } from "worker_threads";
import { Mutex } from "@esfx/threading-mutex";
import { ConditionVariable } from "@esfx/threading-conditionvariable";
import { StructType, int32 } from "@esfx/struct-type";
// NOTE: For TypeScript, the `as const` is needed in the
// declaration below to infer the correct field names.
// For JavaScript, remove the `as const`.
const SharedData = StructType([
{ name: "ready", type: int32 },
{ name: "processed", type: int32 },
] as const);
function worker_thread() {
const m = new Mutex(workerData[0]);
const cv = new ConditionVariable(workerData[1]);
const data = new SharedData(workerData[2]);
m.lock();
try {
// release the lock and wait until main() sends data
cv.wait(m, () => data.ready === 1);
// after waiting we once again own the lock
console.log("worker thread is processing data");
// send data back to main()
data.processed = 1;
console.log("worker thread is done");
}
finally {
m.unlock();
}
cv.notifyOne();
}
function main() {
const m = new Mutex();
const cv = new ConditionVariable();
const data = new SharedData(/*shared*/ true);
// start the Worker, passing the buffers of the shared objects
const worker = new Worker(__filename, {
workerData: [m.buffer, cv.buffer, data.buffer],
stdout: true,
});
// pipe stdout for console.log in worker
worker.stdout.pipe(process.stdout);
// send data to the worker
m.lock();
try {
data.ready = 1;
console.log("main is ready");
}
finally {
m.unlock();
}
// notify the waiting worker
cv.notifyOne();
m.lock();
try {
// release the lock and wait for the worker to finish processing
cv.wait(m, () => data.processed === 1);
}
finally {
m.unlock();
}
}
if (isMainThread) {
main();
}
else {
worker_thread();
}
API
You can read more about the API here.
1.1.0-alpha.202307260322
2 years ago
1.1.0-alpha.202302091715
2 years ago
1.1.0-alpha.202306061406
2 years ago
1.1.0-alpha.202306061316
2 years ago
1.1.0-alpha.202306051228
2 years ago
1.1.0-alpha.202306080253
2 years ago
1.1.0-alpha.202302071935
2 years ago
1.1.0-alpha.202305082104
2 years ago
1.1.0-alpha.202211221509
3 years ago
1.1.0-alpha.202210252139
3 years ago
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-prenative.0
3 years ago
1.0.0-prenative.1
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.1.0-alpha.202211291446
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.23
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