multithreading - Is race condition possible in Javascript? (e.g.: I want to get and set value atomically) -



multithreading - Is race condition possible in Javascript? (e.g.: I want to get and set value atomically) -

i did lot of searching still cannot figure out.

it's simple, global variable has initial value of false;

var globalvar = false;

settimeout used twice phone call same function:

function(){ if(!globalvar){ globalvar = true; alert('show once'); } }

so alert called once? or race status possible?

not in case. javascript run in single threaded environment. 1 of timeouts execute completly before other able execute, meaning when other executes, see globalvar true.

the concurrency issues can happen if wait io, timeout, or that. @ point, other javascript allowed execute. no code ever interrupt other code in order execute.

for example:

settimeout(function () { alert('hi!'); }, 1000); while(true) {}

you never see alert since while loop busy wait, chomping javascript thread other code , never giving timeout chance execute.

javascript multithreading atomic race-condition

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -