admin管理员组

文章数量:1401832

Here's a part of my app.js:

var connections = [];

function removeConnection(res) {
    var i = connections.indexOf(res);
    if (i !== -1) {
        connections.splice(i, 1);
    }
}

And I call removeConnection when a request is closed:

req.on('close', function () {
    console.log("connection closed");
    removeConnection(res);
});

I wonder if the above code is thread safe? I mean as Node.js is event driven, is the following scenario possible?

  1. connections is [a,b,c]
  2. threadB calls removeConnection
  3. in threadB.removeConnection: i = 1
  4. threadA calls removeConnection
  5. in threadA.removeConnection: i = 0
  6. in threadA.removeConnection: connections.splice(0, 1); => connections is [b,c]
  7. in threadA.removeConnection: connections.splice(1, 1); => connections is [b]

As you see in this scenario, threadC's connection would be removed instead of threadB's.

Can this happen? If yes, then how should I fix the code?

Here's a part of my app.js:

var connections = [];

function removeConnection(res) {
    var i = connections.indexOf(res);
    if (i !== -1) {
        connections.splice(i, 1);
    }
}

And I call removeConnection when a request is closed:

req.on('close', function () {
    console.log("connection closed");
    removeConnection(res);
});

I wonder if the above code is thread safe? I mean as Node.js is event driven, is the following scenario possible?

  1. connections is [a,b,c]
  2. threadB calls removeConnection
  3. in threadB.removeConnection: i = 1
  4. threadA calls removeConnection
  5. in threadA.removeConnection: i = 0
  6. in threadA.removeConnection: connections.splice(0, 1); => connections is [b,c]
  7. in threadA.removeConnection: connections.splice(1, 1); => connections is [b]

As you see in this scenario, threadC's connection would be removed instead of threadB's.

Can this happen? If yes, then how should I fix the code?

Share Improve this question edited Dec 28, 2016 at 11:20 Gavriel asked Jul 16, 2014 at 14:49 GavrielGavriel 19.2k12 gold badges72 silver badges115 bronze badges 3
  • 5 There's only a single thread in node.js apps so all node.js code is thread safe. – JohnnyHK Commented Jul 16, 2014 at 14:54
  • @JohnnyHK if that's so, then I would accept this as an answer. Is this something that might be changed in the future? Also adding more cores to a webserver that runs nodejs doesn't help it at all? – Gavriel Commented Jul 16, 2014 at 15:30
  • 1 If you want to take advantage of more cores/processors, you can use child processes manually or use something like the built-in cluster module. – mscdex Commented Jul 16, 2014 at 15:36
Add a ment  | 

1 Answer 1

Reset to default 9

One of the key principles of Node.js is that all user code runs in a single thread which eliminates the need for the developer to deal with the plexities of writing thread-safe code.

So your code (and all Node.js code) is by definition thread-safe.

本文标签: javascriptHow to write thread safe code in nodejsStack Overflow