node.js代码会导致竞态条件吗? [英] Can node.js code result in race conditions?

查看:116
本文介绍了node.js代码会导致竞态条件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,当不同的线程尝试更改共享变量时,就会出现竞争条件,这可能导致这些线程的任何串行执行顺序都无法获得该值.

From what I read, race conditions occur when different threads try to change a shared variable, which can result in a value that's not possible with any serial order of execution of those threads.

但是node.js中的代码在单个线程中运行,那么,这是否意味着用node.js编写的代码没有竞争条件?

But code in node.js runs in a single thread, so, does that mean code written in node.js is free of race conditions?

推荐答案

是.一旦开始共享资源,Node.js可能会陷入竞争状态.

Yes. Node.js can run into race conditions as soon as you start sharing resources.

我也错误地认为您无法在Node.js中获得竞争条件,因为它是单线程性质,但是一旦您在节点外部使用共享资源(例如,文件系统中的文件),您就可以进入比赛条件.当我尝试理解此问题时,我在此问题中发布了此问题的示例: node.js读取文件有问题

I mistakenly also thought you couldn't get race conditions in Node.js because it's single threaded nature, but as soon as you use a shared resource outside of node (e.g. a file from the file system) you can get into a race condition. I posted an example of this issue in this question when I was trying to understand this: node.js readfile woes

Node.js与其他环境的不同之处在于,您只有一个JavaScript执行线程,因此只有一个JavaScript实例在运行您的代码(与在线程环境中相反,在该线程环境中有许多线程在执行您的应用程序代码)同时).

What is different in Node.js from other environments is that you have a single thread of JavaScript execution so there is only one JavaScript instance running your code (as oppossed to a threaded environment in which there are many threads executing your app code at the same time.)

这篇关于node.js代码会导致竞态条件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆