【发布时间】:2019-05-09 14:13:59
【问题描述】:
我遇到了一些我继承的现有代码,它查找并存储存储在 tbb::concurrent_unordered_map 中的结构的引用。我知道如果它是一个迭代器,它会是安全的,但是对实际对象的引用似乎很可疑。
代码不断将新项目插入 tbb::concurrent_unordered_map。插入是否可以不更改 tbb 映射中包含的项目的物理位置,这会使存储的引用指向错误的位置,就像 std::map 一样?
tbb 文档指出:
"与 std::list 一样,插入新项不会使任何项无效 迭代器,也不会更改地图中已有项目的顺序。插入 并且遍历可能是并发的。”
我知道对于 std::list,插入新项目时位置不会改变,但因为 concurrent_unordered_map 文档只讨论了订单,我担心这并没有明确说明它可以移动位置。
下面是一些伪代码,演示了我遇到的代码的概念。
struct MyStruct {
int i;
int j;
};
//some other thread will insert items into myMap
tbb::concurrent_unordered_map <int, MyStruct> myMap;
MyStruct& getMyStruct (int id)
{
auto itr=myMap.find (id);
if (itr!=myMap.end ()) return itr->second;
static MyStruct dummy {1,2};
return dummy;
}
class MyClass {
public:
MyClass (int id)
:m_myStruct {getMyStruct (id)}
{
}
void DoSomething () {
std::cout<<m_myStruct.i<<std::endl;
}
protected:
MyStruct& m_myStruct; //reference to an item held into a tbb::concurrent_unordered_map
};
这段代码已经以相对较高的频率运行了一年多,这似乎表明它是安全的,但我想确定保留对 tbb 中包含的项目的引用是安全的::concurrent_unordered_map。
重写代码需要做很多工作,所以如果可以保持原样,我宁愿不必这样做。
谢谢,
保罗
【问题讨论】: