是stl向量并发读取线程安全吗? [英] Is stl vector concurrent read thread-safe?

查看:498
本文介绍了是stl向量并发读取线程安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,其中大量的线程需要迭代的字符串值的集合,并尝试匹配它自己的数据与列表中的数据。

I am working on a application where huge number of threads are expected to iterate over set of string values and try to match it's own data with the data available in the list.

我正在寻找以下用例:


  1. 向量被初始化为std :: string类型的几个元素。 (让我们说对象名是strList)。

  2. 所有线程将迭代strList,以查看它的值是否与strlist的至少一个元素匹配。

  3. 没有线程会尝试修改strList,它将被严格用作只读对象。

并发读是线程安全的向量对象。我使用RHEL 6和gcc版本是4.5.x

So could you please tell me if concurrent reads are thread-safe on vector object. I am using RHEL 6 and gcc version is 4.5.x

推荐答案

,它是完美的线程安全。

YES for the scenario you mention, it is perfectly Thread Safe.

实际上,STL不是正确的引用方式。 $ b它是 C ++标准库

Actually, STL is not a correct way of referring it.
It is the C++ Standard Library.

C ++ 03标准没有讨论并发性,所以并发方面被忽略为实现细节编译器。因此,编译器附带的文档是应该寻找与并发相关的答案的文档。

The C++03 Standard does not talk about concurrency at all, So the concurrency aspect is left out as an implementation detail for compilers. So the documentation that comes with your compiler is where one should look to for answers related to concurrency.

大多数STL实现线程安全。

但是对于从多个线程大多数STL的实现确实是线程安全的。

Most of the STL implementations are not thread safe as such.
But for concurrent reads of same object from multiple threads most implementations of STL are indeed thread safe.

参考文献

MSDN 说:

MSDN says:


单个对象对于从多个线程读取是线程安全的。例如,给定对象A,可以安全地同时从线程1和线程2读取A.

A single object is thread safe for reading from multiple threads. For example, given an object A, it is safe to read A from thread 1 and from thread 2 simultaneously.

Dinkumware STL-Documentation 说:


多个线程可以安全地读取同一个容器对象。 (在容器对象中有非保护的可变子对象。)

Multiple threads can safely read the same container object. (There are nunprotected mutable subobjects within a container object.)

GCC文档 说:

GCC Documentation says:

我们目前使用 SGI STL 线程安全性定义,其中规定:

We currently use the SGI STL definition of thread safety, which states:


只有在对不同容器的同时访问是安全的,并且对共享容器的同时读访问是安全的的情况下,STL的SGI实现才是线程安全的。 如果多个线程访问单个容器,并且至少有一个线程可能写入,则用户负责确保在容器访问期间线程之间的互斥。

所以从上面的内容中可以看出,是的,它是线程安全的GCC从多个线程并发读取相同的对象。

So from the above, Yes it is thread safe in GCC to have concurrent reads of same object from multiple threads.

注意:GCC的标准库是SGI的STL代码的衍生物。

Note: GCC's Standard Library is a derivative of SGI's STL code.

这篇关于是stl向量并发读取线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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