11

Please consider the following code and the explanation from this Mozilla tutorial "Using web workers":

var myWorker = new Worker('my_worker.js');
myWorker.onmessage = function(event) {
  print("Called back by the worker!\n");
};

Line 1 in this example creates and starts running the worker thread. Line 2 sets the onmessage handler for the worker to a function that is called when the worker calls its own postMessage() function.

The thread is started in the moment the Worker constructor is called. I wonder if there might be a race-condition on setting the onmessage handler. For example if the web worker posts a message before onmessage is set.

Does someone know more about this?

Update:

Andrey pointed out that the web worker should start its work, when it receives a message, like in the Fibonacci example in the Mozilla tutorial. But doesn't that create a new race-condition on setting the onmessage handler in the web worker?

For example:

The main script:

var myWorker = new Worker('worker.js');
myWorker.onmessage = function(evt) {..};
myWorker.postMessage('start');

The web worker script ('worker.js')

var result = [];
onmessage = function(evt) {..};

And then consider the following execution path:

main thread                                  web worker
var worker = new Worker("worker.js");
                                             var result = [];
myWorker.onmessage = ..
myWorker.postMessage('start');
                                             onmessage = ..

The "var result = []" line can be left out, it will still be the same effect.

And this is a valid execution path, I tried it out by setting a timeout in the web worker! At the moment I can not see, how to use web workers without running into race-conditions?!

1

1 Answer 1

6

The answer is that both the main script and the web worker have a MessagePort queue which collects the messages before the initial worker script returns.

For details, see this thread on the WHATWG help mailing list: https://web.archive.org/web/20140401202427/http://lists.whatwg.org/pipermail/help-whatwg.org/2010-August/000606.html

4
  • 1
    The link to the mailing list is now broken. Any idea where to read the thread, or alternately, were there any gotchas?
    – HRJ
    Dec 22, 2015 at 5:59
  • 1
    @HRJ Sorry, I can't find an archived version of the thread. But there are no gotchas, a message queue makes sure that you don't have to worry about race conditions.
    – tsauerwein
    Dec 22, 2015 at 14:42
  • 2
  • I think this comment from a deleted answer is important: "The message simply goes into paren't window's message queue, since the parent window is still executing Javascript." This is not what programmers in multithreaded environments are used to. If I understand that correctly, JS pretends web worker creating creating and settings its onmessage in the same script are atomic - the onmessage will not be called until the current script finishes.
    – Suma
    Jun 23 at 13:47

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.