STL Map 或 HashMaps 线程安全吗? [英] Are STL Map or HashMaps thread safe?

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

问题描述

我可以在不需要锁的情况下在多线程程序中使用映射或哈希映射吗?即它们是线程安全的吗?

Can I use a map or hashmap in a multithreaded program without needing a lock? i.e. are they thread safe?

我想同时在地图中添加和删除.

I'm wanting to potentially add and delete from the map at the same time.

似乎有很多相互矛盾的信息.

There seems to be a lot of conflicting information out there.

顺便说一下,我在 Ubuntu 10.04 下使用 GCC 自带的 STL 库

By the way, I'm using the STL library that comes with GCC under Ubuntu 10.04

就像互联网上的其他人一样,我似乎得到了相互矛盾的答案?

Just like the rest of the internet, I seem to be getting conflicting answers?

推荐答案

您可以安全地执行同步读取操作,即调用 const 成员函数.但是,如果其中一个涉及编写,则不能同时执行任何操作,即非常量成员函数的调用对于容器应该是唯一的,并且不能与任何其他调用混合使用.

You can safely perform simultaneous read operations, i.e. call const member functions. But you can't do any simultaneous operations if one of then involves writing, i.e. call of non-const member functions should be unique for the container and can't be mixed with any other calls.

即您不能从多个线程更改容器.所以你需要使用lock/rw-lock以确保访问安全.

i.e. you can't change the container from multiple threads. So you need to use lock/rw-lock to make the access safe.

这篇关于STL Map 或 HashMaps 线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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