On Thu, Mar 15, 2012 at 16:36, Bjorn <[email protected]> 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?

Because your JS code runs in a single thread. It's inconsequential
when exactly the timer expires because its callback runs before or
after the onMessage callback, never simultaneously. Callbacks don't
interrupt one another either.

-- 
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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to