Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which operations in node are thread safe?

Tags:

node.js

I'm using this approach to store data in a global array hosting an http server where certain requests will manipulate the global array.

I'm kind of worried about running into threading issues with certain operations -- mainly push and splice. I imagine if one request has me iterating over the array and removing items based on a conditional, while another request has me calling .push() on the array that I'll run into issues. Can anyone confirm this?

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).

Update:

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).

http://pastebin.com/HcJHTDFY

I'm using ApacheBench to test, making 1000 parallel requests.

This leads me to believe that Test 1 and Test 3 work fine because nodejs won't execute more than 1 instance of the app.get('/test3'...) callback in parallel 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?).

I'm really just trying to understand what the heck non-blocking I/O model 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.

Also if I'm trying to be non-blocking with my callbacks, should I really be calling setTimeout(code, 1)? Or is there a better way?

like image 751
Langdon Avatar asked Apr 04 '12 23:04

Langdon


People also ask

What is thread-safe operations?

Thread safety is the avoidance of data races—situations in which data are set to either correct or incorrect values, depending upon the order in which multiple threads access and modify the data. When no sharing is intended, give each thread a private copy of the data.

Is NodeJS thread-safe?

This example node-addon-api module creates exposes a single function that creates a thread-safe function and a native thread. The function returns a promise that resolves after the native thread calls into JavaScript ten times.

Which object is thread-safe?

An immutable object is one whose state can't be changed once the object is created. Immutable objects are, by their very nature, thread-safe simply because threads have to be able to write to an object's instance variables to experience a read/write or write/write conflict.

Does NodeJS operate in a single threaded or multi-threaded fashion?

Node. js is Single Threaded, i.e. it executes the code in a single sequence or direction. At a given time, only a single task/ call is executed. Asynchronous and Single-Threaded: Execution doesn't wait for the current request to complete and moves to the next request/call.


1 Answers

All are thread safe.

There are no threads, JavaScript is single threaded, it's impossible for two javascript statements to run at the same time.

As an aside, you shouldn't be using globals anyway because globals are evil

Edit:

Test 2 fails because you're using asynchronous callbacks, which means control goes back to node and it can handle more requests. This creates race conditions as seen.

In node, anything that's not asynchronous blocks. The only asynchronous things you have are setTimeout/setInterval/process.nextTick and any asynchronous IO operations.

One shouldn't manually make computation asychronous. One should just avoid doing too much computation.

I've written an article about What it means to be non-blocking

like image 136
Raynos Avatar answered Oct 13 '22 04:10

Raynos