简体   繁体   English

如果迭代器未因插入而无效,则使用std :: find和C :: insert()线程安全

[英]Is using std::find and C::insert() thread safe if iterators are not invalidated by insert

Suppose I have a container C whose iterators are not invalidated upon C.insert(), can I safely perform a std::find() on the container while a concurrent insert() is being performed? 假设我有一个容器C,其C的迭代器在C.insert()时没有失效,是否可以在执行并发insert()时安全地对容器执行std :: find()? That is, am I guaranteed to find a matching element or C::end(), ignoring the fact that the inserted element may match but std::find() gives me C::end()? 也就是说,是否可以保证找到匹配的元素或C :: end(),而忽略了插入的元素可能匹配但std :: find()给我C :: end()的事实?

No. Although iterators are not invalidated by your mutating operation, it is still a mutating operation, and none of the standard containers are defined to be safe to read in one thread while a mutating operation is taking place in another. 不会。尽管迭代器不会因您的变异操作而无效,但它仍然是变异操作,并且没有定义任何标准容器可以安全地在一个线程中进行读取而在另一个线程中进行变异操作。 Remember that there are still "innards" to your container, all manner of internal state, which may be involved in both operations. 请记住,容器仍然存在“内部情况”,各种内部状态都可能与这两种操作有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM