Question

I am writing a worker thread that calculates layout for a word processor. If the worker is presently running, then a request to recalculate again needs to be queued up (it's more complicated than this sometimes immediately re-starting but this simplistic approach works for this question).

So the code to re-calculate is:

if (isRunning) {
  restart = true;
  return;
}

recalculate();

And the method code is:

recalculate() {
  isRunning = true;
  // perform the recalculation
  isRunning = false;
}

The problem with the above is, what if after the "if" in the first set of code is called, then the recalculate() method completes and sets isRunning to false, then the first set of code completes its run with a return?

Or, if all this code is in the web worker, does calling the method with the first set of code pause the recalculate method until the first set of code completes and returns? If so, then synchronization is automatic between methods?

thanks - dave

Était-ce utile?

La solution

I've written a bunch of test code and it looks like web workers run as follows - when you call a method, that's the only thing that runs until it returns. You can make other calls and they are queued up, but they do not run until the first method completes.

The good news is no worrys about race conditions, synchronization, etc. The bad news is there's no way to talk to a method in a web worker that is crunching away.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top