Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript - event driven and concurrency issues?

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?

like image 696
Daniel Avatar asked Nov 17 '10 21:11

Daniel


1 Answers

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.

like image 109
Amnon Avatar answered Oct 02 '22 12:10

Amnon