Javascript is single threaded. Anything asynchronous that happens, happens 
at the end of the execution stack.

In node, asynchronous calls are queued up, then when they are ready to call 
the callback, they do so in order. They do not get executed in the middle 
of code.

On Thursday, March 15, 2012 8:36:49 AM UTC-7, Bjorn wrote:
>
> Hi!
>
> First off, Node is great. I'm used to writing enterprise C++ servers using 
> the same evented paradigm - and Node is a really compelling and fun 
> alternative. However, the language barrier is a tough one. 
> I have a question, that while it's not directly Node related, it's been 
> brought up by my interest in Node, and I've searched *everywhere* for an 
> answer. Maybe you can help?
>
> Basically, the scenario I have that caused me to wonder was like this 
> (pseudocode) ;
>
> var outputBuffer = "";
>
> SetInterval(function() {
>           outputBuffer += "asdf";
> }, 100);
>
> websocket.onMessage(function() {
>           // reply with outputbuffer
>           send(outputBuffer); // Race condition? 
>           outputBuffer = "";  // What if SetInterval happened?
> });
>
> And the question is; what if SetInterval updates the outputBuffer between 
> the two red lines above? (which would cause me to loose one or more updates 
> since the variable is cleared after the send). In my C++ servers this is a 
> clear racecondition that needs to be protected by any number of means.
> However, I suspect the Javascript event-model sucessfully prohibits the 
> racecondition (as I've seen lots and lots of javascript that potentially 
> has this issue).
>
> How can this code be safe and not have a race condition?
>
> Cheers,
> Bjorn
>
>

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to