C++11 中是否有并发容器? [英] Are there any concurrent containers in C++11?

查看:39
本文介绍了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 中是否有并发容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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