Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.
2412 Discussions

How to iterate over concurrent_hash_map using accessor?

sunholee0127
Beginner
86 Views
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
86 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.
Reply