title: Which operations in node are thread safe?
body: <p>I'm using <a href="https://stackoverflow.com/questions/2219333/nodejs-in-memory-storage">this approach</a> to store data in a global array hosting an http server where certain requests will manipulate the global array.</p>
  
  <p>I'm kind of worried about running into threading issues with certain operations -- mainly <code>push</code> and <code>splice</code>.  I imagine if one request has me iterating over the array and removing items based on a conditional, while another request has me calling <code>.push()</code> on the array that I'll run into issues.  Can anyone confirm this?</p>
  
  <p>I mostly write in C# where even a simple increment isn't thread safe (launching 25 threads that do i++, won't guarantee that i == 25 after all is said and done).</p>
  
  <p><strong>Update</strong>:</p>
  
  <p>I've written 5 examples to demonstrate what I'm talking about.  Test 1 and Test 3 work fine.  Test 2 fails, because of well... what normally would be called threading issues (whether they are actual CPU threads or not).  Test 4 and 5, when run in parallel seem to work (meaning they don't have collision problems like Test 2).</p>
  
  <p><strong><a href="http://pastebin.com/HcJHTDFY" rel="noreferrer">http://pastebin.com/HcJHTDFY</a></strong></p>
  
  <p>I'm using ApacheBench to test, making 1000 parallel requests.</p>
  
  <p>This leads me to believe that Test 1 and Test 3 work fine because nodejs won't execute more than 1 <strike>instance of the <code>app.get('/test3'...)</code> callback in parallel</strike> javascript function in parallel (blocking?).  As soon as you implement a setInterval/setTimeout, it frees up nodejs to execute another instance of the callback (non-blocking?).</p>
  
  <p>I'm really just trying to understand what the heck <code>non-blocking I/O model</code> really means.  Does it mean that "hey it's possible to do non-blocking with setTimeout and setInterval if you need non-blocking, otherwise we're going to block any other outer-level functions from being run until we exhaust the function we're on"?  I feel it's imperative to know this so that I don't get myself into trouble thinking I could implement something like /test2 and be totally safe.</p>
  
  <p>Also if I'm trying to be non-blocking with my callbacks, should I really be calling <code>setTimeout(code, 1)</code>?  Or is there a better way?</p>