I am pretty new to web worker and multi-thread design, what I need to design is a simple query task scheduler (using web worker right now) like:
var taskScheduler = {};
taskScheduler.pool = [];
function init(num){
for(var i=0; i<num; i++){
var workUnit = {};
var worker = new Worker("worker.js");
workUnit.busy = false;
workUnit.worker = worker;
taskScheduler.pool.push( workUnit );
}
}
init(4);
Code below should be loop query workUnit availability and start new work Which I am not quite sure how to implement, I thought it should be something like:
taskScheduler.startWork = function(task){
for(var i=0; i<taskScheduler.pool.length; i++){
if(!taskScheduler.pool.busy){ // fire job, make unit busy then break; }
}
}
Currently the main challenge is:
How do I keep checking worker availability while still be able to accept new task call( for example: if there is no worker available, it will keep asking which will block the ability to accept new job call of taskScheduler.startWork )