Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tbb concurrent hash map iterator

I'm traversing an entire tbb concurrent hash map using an iterator and inspecting each (key,value) pair.

for (MAP::pair = myHashTable.begin(); myHashTable.end(); pair++)

How can I parallelize this iterator?

like image 709
NewToAndroid Avatar asked Mar 19 '23 23:03

NewToAndroid


1 Answers

Use range() method which is described in the Reference Manual:

HashTable_t myHashTable; // assuming HashTable_t is concurrent_hash_map specialization
tbb::parallel_for( myHashTable.range(), [](const HashTable_t::range_type &r) {
    for( HashTable_t::iterator i = r.begin(); i != r.end(); i++);
} );

(I use c++11 for concision but show the types explicitly for sake of c++03)

And please pay attention to the caution note there:

Do not call concurrent operations, including count and find while iterating the table. Use concurrent_unordered_map if concurrent traversal and insertion are required.

like image 112
Anton Avatar answered Mar 27 '23 18:03

Anton