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

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

问题描述

我正在开发一个应用程序,在该应用程序中,需要大量线程迭代一组字符串值,并尝试将其自己的数据与列表中的可用数据进行匹配.

我正在寻找以下用例:

  1. Vector 被初始化为 std::string 类型的几个元素.(假设对象名称是 strList).strList 将在应用程序启动时初始化.
  2. 所有线程将遍历 strList 以查看它的值是否与 strList 的至少一个元素匹配.
  3. 任何线程都不会尝试修改 strList,它会被严格用作只读对象.

所以你能告诉我并发读取在向量对象上是否是线程安全的.我使用的是 RHEL 6,gcc 版本是 4.5.x

解决方案

YES 对于您提到的场景,它是完全线程安全的.

<小时>

实际上,STL 并不是一个正确的引用方式.
它是 C++ 标准库.

C++03 标准根本不讨论并发性,因此并发性方面被省略为编译器的实现细节.因此,您应该在编译器附带的文档中寻找与并发相关的答案.

大多数 STL 实现不是线程安全的.
但是对于来自多个线程的同一对象的并发读取,大多数 STL 实现确实是线程安全的.

参考:

MSDN 说:<块引用>

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

Dinkumware STL 文档 说:

<块引用>

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

GCC 文档 说:

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

<块引用>

STL 的 SGI 实现是线程安全的,仅在对不同容器的同时访问是安全的,对共享容器的同时读取访问是安全的意义上.如果多个线程访问单个容器,并且至少有一个线程可能会写入,则用户有责任确保容器访问期间线程之间的互斥.

所以从上面可以看出,在 GCC 中从多个线程并发读取同一个对象是线程安全的.

注意:GCC 的标准库是 SGI 的 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.

I am looking for following use case:

  1. Vector is initialized with few elements of type std::string. (Lets say object name is strList). strList will get initialized at the time of application startup.
  2. All threads will iterate over strList to see if it's value matches with atleast one element of strList.
  3. No thread will ever try to modify strList and it will be strictly used as readonly object.

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.


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

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.

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.

References:

MSDN says:

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.

The Dinkumware STL-Documentation says:

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

GCC Documentation says:

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

The SGI implementation of STL is thread-safe only in the sense that simultaneous accesses to distinct containers are safe, and simultaneous read accesses to to shared containers are safe. If multiple threads access a single container, and at least one thread may potentially write, then the user is responsible for ensuring mutual exclusion between the threads during the container accesses.

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

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

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

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