As I understand, one of the benefits of NodeJS is that it's one thread per process; in the standard case, you don't need to worry about concurrency.
I also read about NodeJS scaling on multi core machines (Node.js on multi-core machines):
Workers will compete to accept new connections, and the least loaded process is most likely to win. It works pretty well and can scale up throughput quite well on a multi-core box.
In this case, will multiple threads execute in parallel? If so, doesn't that mean we do have to write multithreaded code (if we want to use multiple cores) - and if so, how do I do that?
Or if they don't execute in parallel... where is the boost/benefit of multiple cores coming from?
Edit: My current understanding
So there may be multiple processes on multiple cores but each process only has a single thread.
For example:
var io = require('socket.io').listen(81);
var connections = [];
io.sockets.on('connect', function (socket) {
console.log('connected...');
connections.push(socket);
socket.on('disconnect', function () {
console.log('disconnected');
connections.remove(socket);
});
});
There aren't race connections; there's a single thread, there won't be concurrent accesses of connections
. When you have different processes, each process has its own copy of connections
. So if you had a massive chatroom you couldn't balance the load with multiple processes; each process would be its own chatroom.
In this aspect, it's not any different from PHP, in that each PHP script has its own copy of the variables so you don't write locking code. Of course, the rest of it is completely different, but as far as I can see the argument "you don't have to write thread-locking code" isn't much of a plus because most data will be saved elsewhere anyways (not as in-memory variables).