STL Map 或 HashMap 线程安全吗?

发布于 2024-09-14 03:59:58 字数 179 浏览 1 评论 0原文

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

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

那里似乎有很多相互矛盾的信息。

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

编辑:就像互联网的其他部分一样,我似乎得到了相互矛盾的答案?

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.

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

旧竹 2024-09-21 03:59:58

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

即您无法从多个线程更改容器。所以需要使用lock/rw-lock
以确保访问安全。

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.

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

友欢 2024-09-21 03:59:58

不,

诚实。否。

编辑

好的,我会限定它。

您可以让任意数量的线程读取同一个映射。这是有道理的,因为阅读它没有任何副作用,所以其他人是否也在这样做并不重要。

但是,如果您想对其进行写入,则需要获得独占访问权限,这意味着在您完成之前要阻止任何其他线程写入或读取。

您最初的问题是关于并行添加和删除。由于这些都是写入,因此它们是否线程安全的答案是简单、明确的“否”。

No.

Honest. No.

edit

Ok, I'll qualify it.

You can have any number of threads reading the same map. This makes sense because reading it doesn't have any side-effects, so it can't matter whether anyone else is also doing it.

However, if you want to write to it, then you need to get exclusive access, which means preventing any other threads from writing or reading until you're done.

Your original question was about adding and removing in parallel. Since these are both writes, the answer to whether they're thread-safe is a simple, unambiguous "no".

灰色世界里的红玫瑰 2024-09-21 03:59:58

TBB 是一个免费的开源库,提供线程安全的关联容器。 (http://www.threadingbuildingblocks.org/)

TBB is a free open-source library that provides thread-safe associative containers. (http://www.threadingbuildingblocks.org/)

迷你仙 2024-09-21 03:59:58

STL 容器线程安全最常用的模型是 SGI 模型:

STL 的 SGI 实现仅在以下意义上才是线程安全的:
同时访问不同的
容器是安全的,并且同时
对共享容器的读访问
是安全的。

但最终这取决于 STL 库的作者 - AFAIK 标准没有提到 STL 的线程安全性。

但根据 文档 如果满足许多条件,则 GNU 的 stdc++ 实现遵循它(从 gcc 3.0+ 开始)。

HIH

The most commonly used model for STL containers' thread safety is the SGI one:

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.

but in the end it's up to the STL library authors - AFAIK the standard says nothing about STL's thread-safety.

But according to the docs GNU's stdc++ implementation follows it (as of gcc 3.0+), if a number of conditions are met.

HIH

半世蒼涼 2024-09-21 03:59:58

答案(就像大多数线程问题一样)是它在大多数情况下都可以工作。不幸的是,如果您在调整大小时看到地图,那么您最终会遇到麻烦。所以不。

为了获得最佳性能,您需要多级锁。首先是一个读锁,它允许访问器不能修改映射并且可以由多个线程持有(多个线程读取项目是可以的)。其次是独占的写锁,它允许以不安全的方式修改映射(添加、删除等)。

编辑 读写锁很好,但它们是否比标准互斥锁更好取决于使用模式。在不了解更多信息的情况下我无法推荐。对两者进行分析,看看哪一个最适合您的需求。

The answer (like most threading problems) is it will work most of the time. Unfortunately if you catch the map while it's resizing then you're going to end up in trouble. So no.

To get the best performance you'll need a multi stage lock. Firstly a read lock which allows accessors which can't modify the map and which can be held by multiple threads (more than one thread reading items is ok). Secondly a write lock which is exclusive which allows modification of the map in ways that could be unsafe (add, delete etc..).

edit Reader-writer locks are good but whether they're better than standard mutex depends on the usage pattern. I can't recommend either without knowing more. Profile both and see which best fits your needs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文