Skip to content

Instantly share code, notes, and snippets.

@155martinmoreno
Created June 26, 2018 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 155martinmoreno/85798fe9e04b987016eb6cacb718d386 to your computer and use it in GitHub Desktop.
Save 155martinmoreno/85798fe9e04b987016eb6cacb718d386 to your computer and use it in GitHub Desktop.
const { Worker, isMainThread, workerData } = require('worker_threads');
let currentVal = 0
let intervals = [10,10, 10]
var shared = 0
function counter(id, i){
shared = i
console.log("[", id, "]", "Shared: " + shared)
return i;
}
if(isMainThread) {
console.log("this is the main thread")
for(let i = 0; i < 2; i++) {
let w = new Worker(__filename, {workerData: i});
}
setInterval((a) => currentVal = counter(a,currentVal + 1), intervals[2], "MainThread");
} else {
console.log("this isn't")
setInterval((a) => currentVal = counter(a,currentVal + 1), intervals[workerData], workerData);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment