是从std :: map或std :: set线程安全复制的吗? [英] Is copying from a std::map or std::set thread-safe?
本文介绍了是从std :: map或std :: set线程安全复制的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道对std::se
t或std::map
的多线程读取访问是安全的,但是如何进行像
I know that multi-thread read access to an std::se
t or std::map
is safe, but what about doing copy operations like
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()
进行同步访问时,我锁定了地图,但是我的代码行为异常,并且我想知道是否会导致不稳定.
I lock the maps when I am using .push()
or .pop()
for synchronized access, but my code is behaving erratically and I would like to know if know if this is causing instability.
推荐答案
仅锁定push()
和pop()
是不够的.如果一个线程可能正在从容器中读取而另一个线程正在对其进行修改,则都都需要锁定.
Locking on push()
and pop()
isn't enough. If one thread could be reading from the container while another is modifying it, both need to lock.
这篇关于是从std :: map或std :: set线程安全复制的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文