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?
- connections is [a,b,c]
- threadB calls removeConnection
- in threadB.removeConnection: i = 1
- threadA calls removeConnection
- in threadA.removeConnection: i = 0
- in threadA.removeConnection: connections.splice(0, 1); => connections is [b,c]
- 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?
- connections is [a,b,c]
- threadB calls removeConnection
- in threadB.removeConnection: i = 1
- threadA calls removeConnection
- in threadA.removeConnection: i = 0
- in threadA.removeConnection: connections.splice(0, 1); => connections is [b,c]
- 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
1 Answer
Reset to default 9One 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
版权声明:本文标题:javascript - How to write thread safe code in node.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744239998a2596744.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论