C ++ std容器的线程安全 [英] Thread safety of C++ std Containers

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

问题描述

我在这里阅读了很多关于以下问题的文章:C ++的标准容器(例如"list"或"map")是否是线程安全的,并且所有人都说这不是一般情况.并行读取应该可以,但是并行写入或并行读取和写入可能会导致问题.

I read a lot of posts here with the question if the standard containers for C++ (like "list" or "map" are thread safe and all of them said that it is not in general. Parallel reads should be OK, but parallel writes or parallel reads and writes may cause problems.

现在,我发现在 www.cplusplus.com 上,大多数情况下都可以访问或修改列表操作很安全.

Now I found out that at www.cplusplus.com that accessing or modifying the list during most of the operations is safe.

一些例子:

map ::查找

访问容器(const版本和非const版本都不能修改容器). 没有映射的值被访问:同时访问或修改元素是安全的.

The container is accessed (neither the const nor the non-const versions modify the container). No mapped values are accessed: concurrently accessing or modifying elements is safe.

map ::插入

容器已修改. 同时访问现有元素是安全的,尽管容器中的迭代范围并非如此.

The container is modified. Concurrently accessing existing elements is safe, although iterating ranges in the container is not.

我是否误解了cplusplus.com,还是我还需要了解有关标准容器中线程安全性的其他信息.

Do I missunderstand cplusplus.com or is there anything else what I have to know about thread safety in std containers.

提前谢谢!

PS:我要的是C ++ 03,而不是C ++ 11

PS: I'm asking for C++03 and not for C++11

推荐答案

有关正确的声音.

请注意,如果您修改实际值,则从多个线程访问map中的值也将受到保护.如果您知道两个线程更新了DIFFERENT条目(我不是说要插入/删除),那么这是安全的.

Note that accessing values within the map from multiple threads, if you modify the actual value, will also need to be protected. If you KNOW that two threads update DIFFERENT entries (I don't mean inserting/removing), then it's safe.

这篇关于C ++ std容器的线程安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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