Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>I'm wondering if mutexes/locks are required for data access within Node.js.</p> </blockquote> <p>Nope! Events are handled the moment there's no other code to run, this means there will be no contention, as only the currently running code has access to that internal array. As a side-effect of node being single-threaded, long computations will block all other events until the computation is done.</p> <blockquote> <p>I understand Javascript (and thus Node.js) is single threaded. I'm just not clear on how events are handled. Do events interrupt?</p> </blockquote> <p>Nope, events are not interrupted. For example, if you put a <code>while(true){}</code> into your code, it would stop any other code from being executed, because there is always another iteration of the loop to be run.</p> <p>If you have a long-running computation, it is a good idea to use <a href="http://nodejs.org/docs/latest//api/all.html#all_process_nexttick_callback" rel="noreferrer">process.nextTick</a>, as this will allow it to be run when nothing else is running (I'm fuzzy on this: the example below shows that I'm probably right about it running uninterrupted, probably).</p> <p>If you have any other questions, feel free to stop into <a href="http://webchat.freenode.net/?channels=node.js&amp;uio=d4" rel="noreferrer">#node.js</a> and ask questions. Also, I asked a couple people to look at this and make sure I'm not totally wrong ;)</p> <pre><code>var count = 0; var numIterations = 100; while(numIterations--) { process.nextTick(function() { count = count + 1; }); } setTimeout(function() { console.log(count); }, 2); // //=&gt; 100 // </code></pre> <hr> <p>Thanks to AAA_awright of #node.js :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload