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.....
    }
}
Ken
  • 109
  • 5
  • 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. – Bergi Dec 08 '15 at 05:11
  • Please show us at least the part where you are sending messages to your workers. – Bergi Dec 08 '15 at 05:11
  • 1
    I think you mean "consecutively" since, as @Bergi mentioned, they do run "concurrently" – rgthree Dec 08 '15 at 05:13
  • you could have them both push into a shared worker since you're not getting any MP speedup by queuing anyway – dandavis Dec 08 '15 at 06: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? – Michal Charemza Dec 08 '15 at 18:00
  • Possible duplicate of [Is it possible for a concurrent read/write read/write collision in JavaScript in the browser?](https://stackoverflow.com/questions/27884186/is-it-possible-for-a-concurrent-read-write-read-write-collision-in-javascript-in) – Paul Sweatte Sep 27 '17 at 15:36

0 Answers0