4

Let's assume 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 be run atomically or not? I've read, that JS run on single thread and there can't be any concurrency issues. But the question is still open (at least for me).

Instead of:

doAjaxGetWithCallback(function(){incrementAndModify();});

I could write something like:

doAjaxGetWithCallback(function(){
doSomeCrazyStuffThatDoesNotUseSharedResource();
incrementAndModify();
doSomeOtherCrazyStuffThatDoesNotUseSharedResource();
});
2
  • atomically means what ? i think it will be called three times one after other . Which question is still open ?
    – zod
    Mar 21, 2012 at 15:07
  • I have many thoughts, but not all of them are true.
    – hades
    Mar 21, 2012 at 15:28

3 Answers 3

4

JavaScript in the browser is single-thread (with exception to s), thus you don't have to bother about concurrency. Essentially - every code block is atomic, no matter how long. If incrementAndModify() does some very CPU-intensive time-consuming operations and AJAX response arrives, the callback will not be executed until the current incrementAndModify() finishes and releases the only thread.

This is also the reason why synchronous AJAX calls are discouraged: AJAX request can take some time during which no other code can be executed (execution thread is unnecessarily occupied). This causes the GUI to "freeze", because no other user events are handled.

See also

BTW this:

doAjaxGetWithCallback(function(){incrementAndModify();});

can be written like this:

doAjaxGetWithCallback(incrementAndModify);
1
  • I don't think that your assertion "thus you don't have to bother about concurrency" is true, even if "every code block is atomic", which I didn't know. Concurrency problems can certainly still occur when you have callbacks (or Promises or Observables) which in some way share state. Even if a callback finishes completely before the next one starts, you have to make allowance for not knowing the order in which these callbacks happen. Trivial with one callback, but obviously the complexity can become vast with multiple callbacks. Jun 24, 2017 at 12:03
0

Have no fear this will only ever alert once.

// sync example
var happened = false;

setTimeout(dontDoTwice, 0);
setTimeout(dontDoTwice, 0);
setTimeout(dontDoTwice, 0);

function dontDoTwice() {
  if (!happened) {
    alert("ALERT! ALERT! ALERT!");
    happened = true;
  }
}

Slightly more complex example: http://jsfiddle.net/7BZ6H/1/

4
  • and how would you synchronize in this crazy code if you had MANY timers and one shared resource?
    – hades
    Mar 21, 2012 at 15:28
  • They are still all accessing the same i, it's just the order that they might be called that you need to worry about. JS is pretty simple, you can't and usually don't need to do fancy things like you do in threaded languages to handle this stuff. The way to make this code synchronous is to take it out of the timer. I'm a little unsure as to the problem you're actually trying to solve though, so maybe more details would help me provide a more useful answer. Mar 22, 2012 at 16:23
  • I'm not trying to solve a problem, I'm trying yo prevent the problem ;)
    – hades
    Mar 27, 2012 at 8:30
  • Oooh, you're worried that if(count==0) could be called twice. The answer is no. The callbacks will happen later. I'll update mine with a better example! Mar 27, 2012 at 17:24
0

Yep, the incrementAndModify() function will always run atomically. That's because of the Run-to-Completion feature of javascript.

See Why no concurrency control tool in javascript for more details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.