문제

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();
}
도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top