std :: map的线程安全性,只读操作 [英] Thread safety of std::map for read-only operations

查看:1720
本文介绍了std :: map的线程安全性,只读操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个std :: map,我用来映射值(字段ID)到一个人类可读的字符串。当我的程序在任何其他线程启动之前启动时,该映射被初始化一次,之后它不再被修改。现在,我给每个线程自己的这个(相当大的)映射的副本,但这显然是低效的使用内存,它减慢了程序启动。所以我想给每个线程一个指向地图的指针,但是这会引发一个线程安全问题。

I have a std::map that I use to map values (field ID's) to a human readable string. This map is initialised once when my program starts before any other threads are started, and after that it is never modified again. Right now, I give every thread its own copy of this (rather large) map but this is obviously inefficient use of memory and it slows program startup. So I was thinking of giving each thread a pointer to the map, but that raises a thread-safety issue.

如果我所做的就是从地图读取以下代码:

If all I'm doing is reading from the map using the following code:

std::string name;
//here N is the field id for which I want the human readable name
unsigned field_id = N; 
std::map<unsigned,std::string>::const_iterator map_it;

// fields_p is a const std::map<unsigned, std::string>* to the map concerned.
// multiple threads will share this.
map_it = fields_p->find(field_id);
if (map_it != fields_p->end())
{
    name = map_it->second;
}
else
{
    name = "";
}

这将工作,或从多个std :: map读取有问题线程?

Will this work or are there issues with reading a std::map from multiple threads?

注意:我目前正在使用visual studio 2008,但我希望这个工作在最主要的STL实现。

Note: I'm working with visual studio 2008 currently, but I'd like this to work acros most main STL implementations.

更新:为正确性编辑的代码示例。

Update: Edited code sample for const correctness.

推荐答案

将从多个线程工作,只要你的地图保持不变。

This will work from multiple threads as long as your map remains the same. The map you use is immutable de facto so any find will actually do a find in a map which does not change.

这里是一个相关的链接:http://www.sgi.com/tech/stl/thread%5Fsafety.html

Here is a relevant link: http://www.sgi.com/tech/stl/thread%5Fsafety.html


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

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.

您属于同时读取共享容器类别。

You fall into he "simultaneous read accesses to shared containers" category.

注意:这对于SGI实现是正确的。您需要检查是否使用其他实现。在似乎广泛用作替代的两个实现中,STLPort具有内置的线程安全性,因为我知道。我不知道Apache的实现虽然。

Note: this is true for the SGI implementation. You need to check if you use another implementation. Of the two implementations which seem widely used as an alternative, STLPort has built-in thread safety as I know. I don't know about the Apache implementation though.

这篇关于std :: map的线程安全性,只读操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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