Why don't I have two accessories for the same item in the tbb hash map?

In the code below, if I do not free a1 , the code seems to be stuck in an infinite loop inside the map.find function.

What if I need to search for an element in two different parts of the application?

 #include <iostream> #include "tbb/concurrent_hash_map.h" using namespace std; using namespace tbb; void main() { concurrent_hash_map<int, int> map; concurrent_hash_map<int, int>::accessor a1, a2; map.insert(make_pair(1, 111)); cout << "a1 - " << map.find(a1, 1) << endl; //a1.release(); cout << "a2 - " << map.find(a2, 1) << endl; } 
+8
c ++ tbb
source share
1 answer

Accessory allows access to the recording. This means that a record lock has been received and held by no more than one accessory. You are at a standstill because the same thread is trying to block the same element for writing through different accessors.

If you want to read the data, use const_accessor with find . It will only receive a read lock. Multiple read locks can be received and held without locking.

 concurrent_hash_map<int, int>::const_accessor a1, a2; 
+8
source share

All Articles