C++11 STL 容器和线程安全

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12931787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:46:56  来源:igfitidea点击:

C++11 STL containers and thread safety

c++multithreadingstlc++11

提问by ?imon Tóth

I have trouble finding any up-to-date information on this.

我很难找到任何关于此的最新信息。

Do C++11 versions of STL containers have some level of thread safety guaranteed?

C++11 版本的 STL 容器是否有一定程度的线程安全保证?

I do expect that they don't, due to performance reasons. But then again, that's why we have both std::vector::operator[]and std::vector::at.

由于性能原因,我确实希望它们不会。但话又说回来,这就是为什么我们同时拥有std::vector::operator[]和的原因std::vector::at

采纳答案by Jonathan Wakely

Since the existing answers don't cover it (only a comment does), I'll just mention 23.2.2 [container.requirements.dataraces] of the current C++ standard specificationwhich says:

由于现有的答案没有涵盖它(只有评论可以),我将仅提及当前C++ 标准规范的23.2.2 [container.requirements.dataraces],其中说:

implementations are required to avoid data races when the contents of the contained object in different elements in the same sequence, excepting vector<bool>, are modified concurrently.

vector<bool>同时修改同一序列中不同元素中包含的对象的内容时,需要实现来避免数据竞争,除了。

i.e. it's safe to access distinct elements of the same container, so for example you can have a global std::vector<std::future<int>>of ten elements and have ten threads which each write to a different element of the vector.

即访问同一个容器的不同元素是安全的,例如,您可以拥有std::vector<std::future<int>>十个元素的全局和十个线程,每个线程写入向量的不同元素。

Apart from that, the same rules apply to containers as for the rest of the standard library (see 17.6.5.9 [res.on.data.races]), as Mr.C64's answersays, and additionally [container.requirements.dataraces] lists some non-const member functions of containers that can be called safely because they only return non-const references to elements, they don't actually modify anything (in general any non-const member function must be considered a modification.)

除此之外,与标准库的其余部分相同的规则适用于容器(参见 17.6.5.9 [res.on.data.races]),正如Mr.C64 的回答所说,另外还有 [container.requirements.dataraces]列出了一些可以安全调用的容器的非常量成员函数,因为它们只返回对元素的非常量引用,它们实际上并没有修改任何东西(通常任何非常量成员函数都必须被视为修改。)

回答by Mr.C64

I think STL containers offer the following basic thread-safety guarantee:

我认为 STL 容器提供以下基本的线程安全保证:

  • simultaneous readsof the sameobject are OK

  • simultaneous read/writesof differentobjects are OK

  • 同时读取的的同一个对象都OK

  • 同时读/写操作不同对象确定

But you have to use some form of custom synchronization (e.g. critical section) if you want to do something different, like e.g. simultaneous writes on the same object.

但是如果你想做一些不同的事情,比如在同一个对象上同时写入,你必须使用某种形式的自定义同步(例如临界区)。

回答by NoSenseEtAl

No. Check out PPL or Intel TBB for thread safe STL-like containers.

否。查看 PPL 或 Intel TBB 以获取线程安全的 STL 类容器。

Like others have noted they have usual "multiple reader thread safety" but that is even pre C++11. Ofc this doesnt mean single writer multiple readers. It means 0 writers. :)

就像其他人指出的那样,他们通常具有“多读取器线程安全性”,但这甚至是 C++11 之前的内容。Ofc 这并不意味着单个作者多个读者。这意味着 0 个作者。:)