从一个std ::复制地图或std ::设置线程安全的?(Is copying from a std

2019-10-29 05:27发布

我知道,多线程读取访问到std::se T或std::map这样做拷贝操作一样是安全的,但什么

std::map<int, int> node_info;
int node = 2;
int node_value;

if (node_info.find(node) != node_info.end())
   current_val = map_of_val[node].front();

我当我使用锁定地图.push().pop()的同步访问,但我的代码运行不稳定,我想知道,如果知道这是否是导致不稳定。

Answer 1:

锁定上push()pop()是不够的。 如果一个线程可以从容器中,而另一名修改它来读书, 需要锁定。



文章来源: Is copying from a std::map or std::set thread-safe?