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

safe iteration of concurrent_hash_map

rmeggins
Beginner
583 Views
The comments in the file concurrent_hash_map.h indicate that using iterators, begin(), and end() are not thread safe. What is the recommended way to iterate the items in a concurrent_hash_map in a thread safe way? If the answer is "put a mutex around the code that does the iteration" then I also have to put a mutex around find/insert/remove, which sort of defeats the concurrency. I need a way to access all of the elements in the hash map read only - I don't need to modify the elements or remove them during the iteration.
0 Kudos
6 Replies
RafSchietekat
Valued Contributor III
583 Views
You could read the manual, or search this forum. "How to lock the whole concurrent_hash_map not warping a portion of code with mutex?" is a very recent thread dealing about a suspiciously similar issue.
0 Kudos
rmeggins
Beginner
583 Views
Thanks. I was using tbb 2.2 2.20090809 in Red Hat Enterprise Linux EPEL (Fedora) 6 which does not have concurrent_unordered_map. I see that there is tbb 3.0 in more recent versions of Fedora which has concurrent_unordered_map.
0 Kudos
RafSchietekat
Valued Contributor III
583 Views
Maybe you could disable the package that comes with your distribution and install a more recent one directly from Intel?
0 Kudos
zw_z_
Beginner
583 Views

Can you give me an code example to show how to do safe iteration of concurrent_hash_map? Document "How to lock the whole concurrent_hash_map not warping a portion of code with mutex?" just can't be opened now.

0 Kudos
RafSchietekat
Valued Contributor III
583 Views

It would seem that that other thread contained the suggestion to use concurrent_unordered_map instead.

 

0 Kudos
Anton_M_Intel
Employee
583 Views

If you can afford not using erase(), concurrent_unordered_map is the best choice. Otherwise, there is a hackish way to traverse concurrent_hash_map if certain constraints are preserved: https://software.intel.com/en-us/blogs/2010/05/14/traversing-concurrent_hash_map-concurrently

0 Kudos
Reply