Clearing std::map under a lock vs moving to a temp object
I would recommend using swap
instead of move
. A moved object is not guaranteed to be actually empty or even usable. But with swap
and a freshly created object you are sure of the results:
void func(){ std::map<int, int> temp_map; using std::swap; { std::lock_guard<std::mutex> l(m); swap(my_map, temp_map); }}