问题
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