Community
cancel
Showing results for 
Search instead for 
Did you mean: 
sunholee0127
Beginner
33 Views

How to iterate over concurrent_hash_map using accessor?

Hi
The concurrent operations (count, find, insert, and erase) invalidate any iterators pointing into the affected instance. It is unsafe to use these operations concurrently with any other operation.
so, I want to iterate and erase elements over concurrent_hash_map using accessor (not using iterator)
How can i do that?
[bash]Hash::accessor acc;
for(Hash::iterator iter = map.begin(); iter != map.end() ; ++iter)
{
  map.find(acc, iter->first);
  if( ~~)
  {
     map.erase(acc);
  }
}[/bash]
is that a thread safe?
I wonder that erasing elements invalidate the iterator.
Thanks.
0 Kudos
1 Reply
RafSchietekat
Black Belt
33 Views

If erasing relatively few elements, store relevant keys into a vector, then revisit the map to erase the corresponding elements. If erasing most elements, copy selectively to new map and swap.