I'm trying to make array of workers so I wrote this code.
for(var c=0;c<core;c++){
let worker = new Worker("Html/Worker.js");
worker.postMessage({core:core,W:width,H:height,id:c,px:px,py:py,pz:pz,yaw:yaw,pitch:pitch,D:D,fov:fov});
worker.onmessage=(e)=>{
let gap = this.sH/core*e.data[2];
this.frameBuffer.set(e.data[0],Math.floor(gap)*this.sW*4);
//alert(e.data);
this.depthBuffer.set(e.data[1],Math.floor(gap)*this.sW);
}
this.threads.push(worker);
}
I wonder if it's able to copy worker without multiple initialization (like the code below)
let worker = new Worker("Html/Worker.js");
for(var c=0;c<core;c++){
worker.postMessage({core:core,W:width,H:height,id:c,px:px,py:py,pz:pz,yaw:yaw,pitch:pitch,D:D,fov:fov});
worker.onmessage=(e)=>{
let gap = this.sH/core*e.data[2];
this.frameBuffer.set(e.data[0],Math.floor(gap)*this.sW*4);
this.depthBuffer.set(e.data[1],Math.floor(gap)*this.sW);
}
this.threads.push(worker);
}
does array.push causes deep copying?
No, Array.push
does not cause deep copying.
this.threads.push(worker)
will not modify the worker
you are passing to it but will just add it as-is to the array this.threads
.
This means in your second example there will be only one worker which receives core
many messages and have a single message listener.
Please notice that in both examples you should not access c
in your onmessage
callback because at the time of receiving messages c
will be equal to core
. If you need it in the callback, declare it with let c
instead of var c
to have access to the value at the time of creating the callback ( explanation ).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.