What operations in node are thread safe?

I use this approach to store data in a global array where the HTTP server is located, where certain requests will be processed by the global array.

I am a little worried that I am facing problems with threads with certain operations - mainly push and splice . I assume that if one request requires me to .push() over the array and delete the elements based on the conditional expression, while the other request calls me .push() in the array in which I ran into problems. Can anyone confirm this?

I mainly write in C #, where even a simple increment is not thread safe (running 25 threads that I ++ do not guarantee that I == 25 after everything is said and done).

Update

I wrote 5 examples to demonstrate what I'm talking about. Test 1 and Test 3 work fine. Test 2 fails because ... what will usually be called threading problems (regardless of whether they are actual CPU threads or not). Tests 4 and 5 when running in parallel seem to work (which means they have no collision issues like test 2).

http://pastebin.com/HcJHTDFY

I use ApacheBench for testing, making 1000 concurrent requests.

This makes me think that Test 1 and Test 3 are working fine, because nodejs will not execute more than one instance of the app.get('/test3'...) in the parallel javascript function in parallel (blocking?). Once you implement setInterval / setTimeout, it frees nodejs to execute another callback instance (non-blocking?).

I'm really just trying to figure out what the heck non-blocking I/O model really means. Does this mean that β€œhey, you might not block using setTimeout and setInterval if you need non-blocking, otherwise we are going to block any other external-level functions from starting until we exhaust the function? I feel like this you need to know that I do not get into trouble thinking that I can implement something like / test 2 and be absolutely safe.

Also, if I try not to block my callbacks, should I really call setTimeout(code, 1) ? Or is there a better way?

+6
source share
2 answers

All threads are safe.

There is no thread, JavaScript is single-threaded, it is impossible to simultaneously execute two javascript statements.

As an aside, you should not use global variables since globals are evil

Edit:

Test 2 fails because you use asynchronous callbacks, which means that control returns to node and can handle more requests. This creates the conditions for the race, as can be seen.

In node, everything is not asynchronous. The only asynchronous things you have are setTimeout / setInterval / process.nextTick and any asynchronous I / O.

Do not manually make the calculations asynchronous. You just need to avoid too much computing.

I wrote an article on what it means to be unblockable

+10
source

Raynos is basically correct. But just because JavaScript is single-threaded doesn't mean you can let your guards go down. You still need to know about threads. A good example to illustrate this is how socket.io maintains connections with different clients and can still track these clients.

Part of the functional programming of JavaScript works with variable binding (e.g. ... LOT). Unaware of threads, you mistakenly believe that your variables are correctly connected. As an example of socket.io, how can you be sure that the connection you receive is the client that you think you are receiving? What should I do if the link was incorrect and the connection really refers to another client?

This is the type of thing you need to observe.

-5
source

Source: https://habr.com/ru/post/912444/


All Articles