【发布时间】:2019-03-16 06:58:38
【问题描述】:
我是 C++ 多线程编程的新手。我从网上阅读了一些文档和一些文章,并注意到在 STL unordered_map 中允许同时进行多次查找操作,而不允许进行多次插入操作。
我有两个问题:
-
可以同时进行查找操作和插入操作吗?例如,
std::string x = "jim"; std::string y = "marry"; std::unordered_map<std::string, int> map; // Thead A executes below code auto iterator = map.find(x); // Thread B executes below code map.insert(std::make_pair(y, "value"));请注意这里的 x 和 y 不相同。
如果 x 和 y 相同怎么办?如果我们找到密钥并同时插入相同的密钥会发生什么?
谢谢各位,如果您回答这个问题,请参考您获得这些知识的参考资料。
【问题讨论】:
-
linkLalaland 的回答实际上解决了我的问题。但是在我提出这个问题之前,由于没有参考,我无法从他的回答中确定规则的正确性。 @kmdredo 提供了参考,消除了我的疑虑。感谢关注这个问题的人:)
标签: c++ multithreading stl unordered-map