Question

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();
}
Was it helpful?

Solution

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

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