Search code examples
multithreadingdata-structurestbbconcurrenthashmap

TBB ThreadingBuildingBlocks strange behaviour


My Question: Why is my program freezing if i use "read only" const_accessors?

It seems to be locking up, from the API description it seems to be ok to have one accessors and multiple const_accessors, (writer, reader). Maybe somebody can tell me a different story.

The Goal i try to achieve is to use this concurrent hash map and make it available to 10-200 Threads so that they can lookup and add/delete information. If you have a better solution than the current one i' am using than you also welcome to post the alternatives.

tbb::size_t hashInitSize = 1200000;
concurrent_hash_map<long int,char*> hashmap(hashInitSize);
cout << hashmap.bucket_count() << std::endl;

long int l = 200;
long int c = 201;

    concurrent_hash_map<long int,char*>::accessor o;
    concurrent_hash_map<long int,char*>::const_accessor t;
    concurrent_hash_map<long int,char*>::const_accessor h;

    cout << "Trying to find 200 "<< hashmap.find(t,200) << std::endl;

    hashmap.insert(o,l);
o->second = "testother";

TBB Community Tutorial Guide Page 43 describes the concept of accessors


Solution

  • From the TBB reference manual:

    An accessor acts as a smart pointer to a pair in a concurrent_hash_map. It holds an implicit lock on a pair until the instance is destroyed or method release is called on the accessor.

    Accessors acquire a lock when they are used. Multiple accessors can exist at the same time. However, if a program uses multiple accessors concurrently and does not release the locks, it is likely to deadlock.

    To avoid deadlock, release the lock on a hash map entry once you are done accessing it.