为std ::向量或升压:: vector的线程安全的? [英] Is std::vector or boost::vector thread safe?

查看:105
本文介绍了为std ::向量或升压:: vector的线程安全的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个线程同时调用的push_back() 的std ::矢量的共享对象。为的std ::矢量线程安全吗?或者我需要实现我自己的机制,使其线程安全的?结果
我想避免做额外的锁定和释放的工作,因为我是一个图书馆的用户,而不是库设计师。我希望找矢量现有线程安全的解决方案。如何的boost ::矢量,这是由新引进提振的 1.48.0 的前进。难道是线程安全的?

I have multiple threads simultaneously calling push_back() on a shared object of std::vector. Is std::vector thread safe? Or do I need to implement the mechanism myself to make it thread safe?
I want to avoid doing extra "locking and freeing" work because I'm a library user rather than a library designer. I hope to look for existing thread-safe solutions for vector. How about boost::vector, which was newly introduced from boost 1.48.0 onward. Is it thread safe?

推荐答案

C ++标准使得在标准C ++库中的所有类特定线程的保证。这些保障可能不是你期望他们是什么,但所有标准C ++库类的某些线程安全的保证制成。请务必阅读制成,虽然担保,作为标准C ++容器不通常,你会想他们是什么对齐的线程保证。对于不同的,通常更强一些类,担保是由以下具体的答案适用于容器。容器主要有以下线程安全保证:

The C++ standard makes certain threading guarantees for all the classes in the standard C++ library. These guarantees may not be what you'd expect them to be but for all standard C++ library classes certain thread safety guarantees are made. Make sure you read the guarantees made, though, as the threading guarantees of standard C++ containers don't usually align with what you would want them to be. For some classes different, usually stronger, guarantees are made and the answer below specifically applies to the containers. The containers essentially have the following thread-safety guarantees:


  1. 可以有相同的容器的多个并发读者

  2. 如果有一个作家,不得有更多的作家和读者没有

这些通常不是什么人想的线程安全保证,但是是非常合理的给出的标准集装箱的接口:他们的目的是要在没有多线程访问的有效利用。添加任何形式的锁定他们的方法将这种干扰。除此之外,容器的接口是没有任何形式的内部锁定的真正有用的:被用于一般多个方法和访问依赖于previous访问的结果。例如,在已经检查了一个容器不是空()元素可能被访问。然而,随着内部锁定但不保证该对象是静止的时候它被实际访问的容器

These are typically not what people would want as thread-safety guarantees but are very reasonable given the interface of the standard containers: they are intended to be used efficiently in the absence of multiple accessing threads. Adding any sort of locking for their methods would interfere with this. Beyond this, the interface of the containers isn't really useful for any form of internal locking: generally multiple methods are used and the accesses depend on the outcome of previous accesses. For example, after having checked that a container isn't empty() an element might be accessed. However, with internal locking there is no guarantee that the object is still in the container when it is actually accessed.

要满足这给上述保证你可能就需要使用某种形式的外部锁定为并行访问容器的要求。我不知道升压容器,但如果他们有类似的标准集装箱的界面我会怀疑他们有相同的保证。

To meet the requirements which give the above guarantees you will probably have to use some form of external locking for concurrently accessed containers. I don't know about the boost containers but if they have an interface similar to that of the standard containers I would suspect that they have exactly the same guarantees.

担保和要求17.6.4.10给出[res.on.objects]第1款规定:

The guarantees and requirements are given in 17.6.4.10 [res.on.objects] paragraph 1:

程序的行为是不确定的,如果从不同的线程标准库函数的调用可能会引入数据竞争。在其下,这可能会发生的条件在17.6.5.9指定。 [注:修改即线程之间共享的标准库类型的对象风险不确定的行为,除非该类型的对象被明确指定为无数据共享的种族或用户提供了锁定机制。 -endnote]

The behavior of a program is undefined if calls to standard library functions from different threads may introduce a data race. The conditions under which this may occur are specified in 17.6.5.9. [ Note: Modifying an object of a standard library type that is shared between threads risks undefined behavior unless objects of that type are explicitly specified as being sharable without data races or the user supplies a locking mechanism. —endnote]

...和17.6.5.9 [res.on.data.races。本节主要详细说明了不是更正规的描述。

... and 17.6.5.9 [res.on.data.races]. This section essentially details the more informal description in the not.

这篇关于为std ::向量或升压:: vector的线程安全的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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