How to iterate over concurrent_hash_map using accessor?

How to iterate over concurrent_hash_map using accessor?

sunholee0127's picture

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?
Hash::accessor acc;
for(Hash::iterator iter = map.begin(); iter != map.end() ; ++iter)
{
  map.find(acc, iter->first);
  if( ~~)
  {
     map.erase(acc);
  }
}
is that a thread safe? I wonder that erasing elements invalidate the iterator. Thanks.
2 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.
Raf Schietekat's picture

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.

Login to leave a comment.