Suppose I have simple code;
var counter = 0; var sharedResource = [];//some shared resource function incrementAndModify(){ if(counter == 0) { sharedResource.push(Math.random());//very sensitive data counter++; } } function init(){ incrementAndModify(); doAjaxGetWithCallback(function(){incrementAndModify();}); incrementAndModify(); }
So the question is: will incrementAndModify () function run atomically or not? I read that JS runs on a single thread and there can be no problems with concurrency. But the question is still open (at least for me).
instead
doAjaxGetWithCallback(function(){incrementAndModify();});
I could write something like:
doAjaxGetWithCallback(function(){ doSomeCrazyStuffThatDoesNotUseSharedResource(); incrementAndModify(); doSomeOtherCrazyStuffThatDoesNotUseSharedResource(); });
hades source share