Greetings,
I've been studying javascript, nodejs. And I don't understand how the concurrency issues are avoided in javascript.
Lets say I'm working on a object
var bigObject = new BigObject();
and I have a setTimer(function(){ workOnBigOjbect...} )
that will also do work on bigOjbect
.
If I have disk IO being written into bigObject
, and a timer object working on bigObject
, and regularly code reading from bigObject
, how are concurrency issues avoided?
In a regular language, I would use a mutex or thread-safe queue/command pattern. I also don't see much discussion about race conditions for javascript.
Am I missing something?
The whole point of node.js is that it's event-driven. All the code runs in event handlers in a single thread. There are no concurrency issues because the code doesn't run concurrently. The downside is that each event handler must exit quickly because it blocks the other events.
In your example, the code will start the disk IO and exit immediately. The node.js infrastructure will notify the program that the IO operation was completed by running an event handler. The timer event will be called before or after the IO event, but never concurrently.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With