1

I currently have 2 web workers setup like this:

gpsThread = new Worker("js/workers/gpsd_poll.js");
gpsStarted = true;

gpsThread.onmessage = function(event) {
    if (event.data !== "") {
         do lots of stuff here.....
    }
}

netThread = new Worker("js/workers/net_poll.js");
netStarted = true;

netThread.onmessage = function(event) {
    if (event.data !== "") {
         do lots of stuff here.....
    }
}

When gpsThread receives a message and processing, netThread must wait until gpsThread has completed before it begins its callback.

Is there anyway to make them run 'concurrently'? I have tried nesting them together like this:

gpsThread = new Worker("js/workers/gpsd_poll.js");
gpsStarted = true;

netThread = new Worker("js/workers/net_poll.js");
netStarted = true;

gpsThread.onmessage = function(event) {
    netThread.onmessage = function(event) {
        if (event.data !== "") {
             do lots of stuff here.....
        }
    }

    if (event.data !== "") {
         do lots of stuff here.....
    }
}
6
  • Web workers do run concurrently, there's no way to prevent that. They can only communicate via asynchronous events. As usual, you cannot "wait" unless you use a callback. Commented Dec 8, 2015 at 5:11
  • Please show us at least the part where you are sending messages to your workers. Commented Dec 8, 2015 at 5:11
  • 1
    I think you mean "consecutively" since, as @Bergi mentioned, they do run "concurrently" Commented Dec 8, 2015 at 5:13
  • you could have them both push into a shared worker since you're not getting any MP speedup by queuing anyway Commented Dec 8, 2015 at 6:03
  • Maybe you could post some sort of pseudocode/flow diagram that shows the order of events / flow of work or data, in order to clarify how you would like the code to work? Commented Dec 8, 2015 at 18:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.