Is copying from a std::map or std::set thread-safe?

☆樱花仙子☆ 提交于 2019-12-13 06:12:22

问题


I know that multi-thread read access to an std::set 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();

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.


回答1:


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.



来源:https://stackoverflow.com/questions/17669480/is-copying-from-a-stdmap-or-stdset-thread-safe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!