- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我知道标准库中的容器不是线程安全的。我曾经认为一个容器,比如 std::list
类型的容器,不能被多个线程同时访问(其中一些可能会修改容器)。但现在看来,它的意义远不止眼前所见;更微妙的东西,不是那么明显的东西,至少对我来说是这样。
例如,考虑这个接受第一个参数的函数按值:
void log(std::string msg, severity s, /*...*/)
{
return; //no code!
}
这是线程安全的吗?
起初,它似乎是线程安全的,因为函数体不访问共享的可修改资源,因此是线程安全的。再想一想,当我调用这样一个函数时,会创建一个 std::string
类型的对象,这是第一个参数,我认为这个对象的构造不是t 线程安全,因为它在内部使用 std::allocator
,我认为这不是线程安全的。因此调用这样的函数也不是线程安全的。但如果它是正确的,那么这个呢:
void f()
{
std::string msg = "message"; //is it thread-safe? it doesn't seem so!
}
我走对了吗?我们可以在多线程程序中使用 std::string
(或任何内部使用 std::allocator
的容器)吗?
我专门将容器称为局部变量,而不是共享对象。
我搜索了谷歌,发现了很多类似的疑问,没有具体的答案。我面临与他类似的问题:
请同时考虑 C++03 和 C++11。
最佳答案
在 C++11 中,std::allocator
是线程安全的。从它的定义来看:
20.6.9.1/6: Remark: the storage is obtained by calling
::operator new(std::size_t)
从::operator new的定义
:
18.6.1.4: The library versions of
operator new
andoperator delete
, user replacement versions of globaloperator new
andoperator delete
, and the C standard library functionscalloc
,malloc
,realloc
, andfree
shall not introduce data races (1.10) as a result of concurrent calls from different threads.
C++03 没有线程的概念,因此任何线程安全都是特定于实现的;您必须引用您的实现文档以查看它提供的保证(如果有)。由于您使用的是 Microsoft 的实现,this page表示从多个线程写入同一类的多个容器对象是安全的,这意味着 std::allocator
是线程安全的。
关于c++ - 标准容器作为多线程应用程序中的局部变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9521879/
有人可以解释一下为什么我得到: "use of unassigned local variable number_of_column" for: if (i f.LastWriteTime).Fir
我正在尝试为查询定义和初始化 MySQL 变量。 我有以下几点: declare @countTotal int; SET @countTotal = select COUNT(*) from nG
局部变量由小写字母或下划线(_)开头.局部变量不像全局和实变量一样在初始化前含nil值. ruby>$foo nil ruby>@foo nil ruby>foo ER
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
当我单击 Login 类上的注册按钮时,出现 nullpointerException,它给出了该错误。我尝试修改本地和全局变量,但似乎没有任何方法可以修复该错误,我可能在 onClickListen
我之前看过一些关于此的帖子,但我一直无法找到有关 actionListeners 的帖子。我正在尝试使用 JButton 数组创建井字棋。如果可能的话,如何在使用 for 循环临时变量的同时向它们添加
我试图找出一种将 getView() 方法中的位置变量传递给内部类的方法。但是,这不能是最终变量,因为 ListView 中的每个项目都会调用 getView() ,因此它会发生变化。有没有办法访问该
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve th
这对你们中的某些人来说似乎微不足道,但我对下面的这两个示例感到困惑。 int[] numbers = new int[] { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; int i
这个问题在这里已经有了答案: How do JavaScript closures work? (86 个答案) 关闭 7 年前。 所以我正在复习我的 vanilla Javascript,专门用于
我正在将mockito与spring(java 1.8)一起使用,并且我尝试在我的Answer对象中使用局部变量: public IProductDTO productForMock = null;
是否可以在java中为静态方法注入(inject)局部变量,比如 @Inject public void someMethod() { @MyInjectQualifier MyObjectC
我有一个函数,每 2 秒被重复调用一次,每次从屏幕顶部带来一个具有随机纹理的球。我希望能够在 touchesBegan 中使用这个球,但我不能,因为它是一个局部变量。我试过将它设为全局变量,但这给了我
这是(我假设)一个基本问题,但我似乎无法弄清楚。 给定以下代码: from src.Globals import * import pygame # Used to manage how fast t
这就是我在循环中引用全局变量的方法。 _.forEach(myTableName.detailsObjects, function (o, key) { if
我已经创建了一些代码: import numpy as np Length=(2.7)*10**-3 Nx=4 x = np.linspace(0, Length, Nx+1) # mes
如何获取局部变量? 我有这个代码 if (ctrl is Control) { Control c = (Control)ctrl; foreach (object innerCtrl
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Difference between class variables and class instance
我正在学习 Python 3,我有一个关于 Python 中面向对象编程的非常基本的问题。这是我的代码。 class pet: number_of_legs = 0 def count
我有以下代码块: class Student{ int age; //instance variable String name; //instance varia
我是一名优秀的程序员,十分优秀!