Simple Worker Thread
NodeJS
Hard
3 views
Problem Description
Run heavy work in worker_threads and return result to main thread.
Output Format
Print result.
Constraints
Single-file demo using eval worker.
Official Solution
const { Worker, isMainThread, parentPort } = require('worker_threads');
if (!isMainThread) {
let s = 0;
for (let i = 0; i < 8e7; i++) s += i % 7;
parentPort.postMessage('done');
} else {
const w = new Worker(__filename);
w.on('message', (m) => console.log(m));
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!