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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With