Javascript in Firefox is single-threaded, so you don't need to worry
about other scripts running, unless you specifically stop execution and
continue later (via an ajax call, a setTimeout, event handler, etc).
So, if you I.E. get a value (let's say a lock) that is false,
immediately set it true and _then_ start doing your work (which can
include the "stopping" methods mentioned above), no other script is
going to execute between your get and set.
Do you have any evidence to back up this statement? I do not believe it
to be true.
Yes. Try this:
http://arantius.info/looper.html
Open one window with one tab, with this page. Click 'start' and you'll
see that
A) Your CPU (or one of its cores) pegs to 100% utilization.
B) The entire browser becomes very unresponsive.
The browser is unresponsive because it's written in XUL+JS, and its JS
can't execute while JS in the page is executing -- there's only one
global thread.
You should note that the time between "end" (of one loop) and "start"
(of the next) that it reports is very small, probably well under 10ms.
If you can, refresh the page so it isn't running anymore (or force kill
and restart the browser). Open a second window with this page. Click
start in one, see the very small delays. Click start in the other, and
you'll see the delay between loops jumps to at least 1 second, in my
case around 1.2 seconds. The JS in one window has to wait for the JS in
the other window to loop, before it gets to execute again.
Or, to repeat myself, with proof this time:
Javascript in Firefox is single threaded. Across all tabs, windows,
etc. You have to start an entire separate instance of Firefox (and that
must be on a separate profile because of how Firefox works) to get
another concurrent thread.
P.S. Web worker threads [1] are an exception to this. You'll note the
restrictive API they get, however. That's how they work. JS is single
threaded in Firefox, basically because there's so much legacy JS that is
not single threaded.
P.P.S. You might also note that Chrome does not exhibit this particular
behavior. Each of Chrome's tabs/windows execute in a separate OS-level
process, and do not block each other for execution. I'm not sure how it
handles the potential problem of cross-window access.