Hi does anyone know how to modify a same array by using 2 worker_threads in node js?
I add value in worker thread 1 and pop it in worker thread 2, but worker thread 2 can't see the value added by 1.
//in a.js
const {isMainThread, parentPort, threadId, MessageChannel, Worker} = require('worker_threads');
global.q = [1,2];
exports.setter_q= function(value){
q.push(value);}
exports.getter_q=function(value){
var v=q.pop()
return v;
}
if(isMainThread) {
var workerSche=new Worker("./w1.js")
var workerSche1=new Worker("./w2.js")
}
//in w1.js
const {isMainThread, parentPort, threadId, MessageChannel, Worker} = require('worker_threads');
if(isMainThread){
// do something
} else{
var miniC1=require("./a.js")
miniC1.setter_q(250);
// do something
}
//in w2.js
const {isMainThread, parentPort, threadId, MessageChannel, Worker} = require('worker_threads');
if(isMainThread){
// do something
} else{
var miniC1=require("./a.js")
var qlast=miniC1.getter_q();
// do something
}
qlast variable in w2.js file is always value '2' instead of 250.
SharedArrayBufferthat you can then access from multiple threads. You will have to then manage concurrency properly so you aren't attempting to update the data simultaneously from more than one thread.