Reputation: 6282
Is javascript running on single thread? If I declare a global array, and start changing it (eg removing elements) on AJAX callback and at the same time start changing the very same array in another function (called with SetTimeOut) - is there a risk of a race condition?
I found this thread: javascript race condition, someone said race conditions never happen in javascript because it always runs in single thread and single callstack. Does this depend on how browser implements it or is it guaranteed to be always single-threaded on all browsers?
Upvotes: 4
Views: 3311
Reputation: 35407
JavaScript is single threaded.
The referenced post discusses differences of setTimeout
on different machines...
HTML5 introduces the concept of WebWorkers
which executes JavaScript on multiple background threads. Though it is not supported on all browsers...
https://developer.mozilla.org/En/Using_web_workers
http://dev.w3.org/html5/workers/
Upvotes: 2
Reputation: 16728
is it guaranteed to be always single-threaded on all browsers?
Yes.
Of course, things like HTTP requests might work in different threads behind the scenes, but when your Javascript code is executed it can only happen from one thread at a time.
Upvotes: 4