1

I have a list of items to insert into tbb's concurrent hash map. What's the correct way of using an accessor, way 1 or 2?

// way 1
for (a list of (keys,values))
{
    MAP::accessor a;

    myHashTable.insert(a, key);
    (a->second).push_back(value);

    a.realease();
}

// way 2

MAP::accessor a;

for (a list of (keys,values))
{ 
    myHashTable.insert(a, key);
    (a->second).push_back(value);

    a.realease();
}

1 Answer 1

4

Basically either, since you explicitly call to accessor::release(). But generally, from the code quality point of view, I'd limit the scope of the locking to minimal necessary region since the code could be extended further in unexpected way or/and an exception-safety could be an issue.

The third way without explicit release is:

// way 3
for (a list of (keys,values))
{
    MAP::accessor a;

    myHashTable.insert(a, key);
    (a->second).push_back(value);
}

P.S. Try to avoid using of accessor in the serial code when possible, e.g. use insert(value_type) form. It will help to reduce the overheads of thread-safety

2
  • If I adopt way 3, wouldn't tbb hold a lock on the object until I explicitly release it or the thread dies? Commented Apr 8, 2014 at 6:21
  • It follows RAII idiom thus it will be released in the destructor at the end of scope for 'a' object.
    – Anton
    Commented Apr 8, 2014 at 8:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.