Question

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?

Was it helpful?

Solution

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 for concision but show the types explicitly for sake of )

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top