Are there any concurrent containers in C++11?(C++11 中是否有并发容器?)
问题描述
特别是,我正在寻找阻塞队列.C ++ 11中有这样的东西吗?如果没有,我的其他选择是什么?我真的不想再深入到线程级别了.太容易出错了.
In particular, I am looking for a blocking queue. Is there such a thing in C++11? If not, what are my other options? I really don't want to go down to the thread level myself anymore. Way too error-prone.
推荐答案
根据 Diego Dagum来自 Microsoft 的 Visual C++ 团队:
一个反复出现的问题(好吧,其中之一)是关于 STL 容器的以及它们是否是线程安全的.
A recurrent question (well, one of the many) is about STL containers and whether they are thread safe.
在这里用 Stephan 的话来说,事实是它们不是,不是作为bug 但作为一个特性:拥有每个 STL 的每个成员函数容器获取内部锁会破坏性能.作为一个通用的、高度可重用的库,它实际上不会提供正确性之一:放置锁的正确级别是由程序在做什么决定.从这个意义上说,个人成员函数往往不是那么正确的级别.
Taking Stephan’s words here, the reality is that they aren’t, not as a bug but as a feature: having every member function of every STL container acquiring an internal lock would annihilate performance. As a general purpose, highly reusable library, it wouldn’t actually provide correctness either: the correct level to place locks is determined by what the program is doing. In that sense, individual member functions don’t tend to be such correct level.
并行模式库 (PPL) 包括几个容器提供对其元素的线程安全访问:
The Parallel Patterns Library (PPL) includes several containers that provide thread-safe access to their elements:
- concurrent_vector Class 是一个序列容器类,允许随机访问任何元素.它支持并发安全的追加、元素访问、迭代器访问和迭代器遍历操作.
- concurrent_queue Class 是一个序列容器类,它允许首先-in, first-out 访问其元素.它支持一组有限的并发安全操作,例如 push 和 try_pop,仅举几例.
- The concurrent_vector Class is a sequence container class that allows random access to any element. It enables concurrency-safe append, element access, iterator access and iterator traversal operations.
- The concurrent_queue Class is a sequence container class that allows first-in, first-out access to its elements. It enables a limited set of concurrency-safe operations, such as push and try_pop, to name a few.
一些示例此处.
也很有趣:http://www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe-queue-using-condition-variables.html.
这篇关于C++11 中是否有并发容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++11 中是否有并发容器?
基础教程推荐
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 为什么语句不能出现在命名空间范围内? 2021-01-01