问题描述
在node.js相关的Web文档中读取它是单线程服务器。因此,默认情况下,所有数据结构在节点服务器中是否都是线程安全的,这让我感到困惑!
Read in a node.js related web document that it is a single threaded server. So it confuses me whether all data structures by default be thread-safe in a node server!
我有多个回调访问这样的全局对象:
I have multiple call-backs accessing a global object like this :
callback1{
global_var['key'] = val;
}
callback2{
globalv_var['key'] = val;
}
'key'有时可能相同,也可能不同。 global_var是否是线程安全的?
回调,按照预期在没有特定顺序的情况下被调回。
'key' may be same at times and may be different as well. Will the global_var be thread-safe ?callbacks, as intended gets called back as and when something is done, in no particular order.
推荐答案
节点。 JS包含一个调度程序。它接受Web请求并将其移交给异步处理。该调度程序是单线程的。但是调度程序为每个任务旋转一个新线程并迅速将任务移交给新线程,释放调度程序的线程以便为新请求提供服务。
Node.JS contains a "dispatcher." It accepts web requests and hands them off for asynchronous processing. That dispatcher is single threaded. But the dispatcher spins up a new thread for each task, and quickly hands off the task to the new thread, freeing the dispatcher's thread for servicing a new request.
如果这些任务线程保持独立(即它们不会修改彼此的状态),是的,它们是线程安全的。
To the extent that those task threads are kept separate (i.e. they don't modify each other's state), yes, they are threadsafe.
这篇关于nodejs数据结构是否是设计线程安全的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!