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

查看:270
本文介绍了在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.

推荐答案

根据Microsoft Visual C ++团队的Diego Dagum,

According to Diego Dagum from Microsoft's Visual C++ Team:


周期性的问题(很多,其中许多)是关于STL容器
和他们是否是线程安全。

A recurrent question (well, one of the many) is about STL containers and whether they are thread safe.

在这里采取斯蒂芬的话,现实是,不是作为一个
错误,但作为一个特点:拥有每个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类是允许对任何元素进行随机访问的序列容器类。它允许并发安全的附加,元素访问,迭代器访问和迭代器遍历操作。

  • concurrent_queue类是一个序列容器类,允许先进先出访问其元素。它允许一系列并发安全的操作,例如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.

一些示例此处

也很有趣:

Also interesting: http://www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe-queue-using-condition-variables.html.

这篇关于在C ++ 11中是否有任何并发​​容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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