gpt4 book ai didi

c++ - C++11 中有并发容器吗?

转载 作者:IT老高 更新时间:2023-10-28 13:58:59 25 4
gpt4 key购买 nike

特别是,我正在寻找一个阻塞队列。 C++ 11中有这样的事情吗?如果没有,我的其他选择是什么?我真的不想再自己下降到线程级别了。太容易出错了。

最佳答案

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

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

Taking Stephan’s words here, the reality is that they aren’t, not as abug but as a feature: having every member function of every STLcontainer acquiring an internal lock would annihilate performance. Asa general purpose, highly reusable library, it wouldn’t actuallyprovide correctness either: the correct level to place locks isdetermined by what the program is doing. In that sense, individualmember functions don’t tend to be such correct level.

The Parallel Patterns Library (PPL) 包括几个提供对其元素的线程安全访问的容器:

  • concurrent_vector Class是一个允许随机访问任何元素的序列容器类。它支持并发安全追加、元素访问、迭代器访问和迭代器遍历操作。
  • concurrent_queue Class是一个序列容器类,它允许对其元素进行先进先出的访问。它支持一组有限的并发安全操作,例如 push 和 try_pop 等等。

一些 sample here .

也很有趣:http://www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe-queue-using-condition-variables.html .

关于c++ - C++11 中有并发容器吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7817364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com