Reputation: 989
How is node handling events? One at the time, or concurrent?
I need to know, if there is going to be concurrent access to a shared array, like in the following example:
var ws = require("./ws.js"),
connections = [];
ws.createServer(function( socket ){
// add to connection array
socket.on('connect', function(){
connections.push(socket);
});
// remove from connection array
socket.on('close', function(){
var i = connections.indexOf(socket);
connections.splice(i,1);
});
}).listen(8000);
When a client connects, its socket is pushed to the array. When the connection is closed, i want to remove it from the connections array.
But, in other languages this could lead to concurrency issues.
Fx. If two connections is closed at the same time:
Will this ever be a problem, or can i assume that only one callback is handled at a time?
Upvotes: 6
Views: 3132
Reputation: 3939
No, this will not be a problem, because your node.js code is executed in a single thread. You will not have the same function called simultaneously.
Upvotes: 9