如何从TBB concurrent_hash_map C++迭代和擦除

How to iterate and erase from TBB concurrent_hash_map C++

本文关键字:C++ 迭代 擦除 map hash TBB concurrent      更新时间:2023-10-16

假设我有一个TBB concurrent_hash_map定义如下:

tbb::concurrent_hash_map<int, void*> TbbHash;

现在我想安全地在 TbbHash 中的所有元素上运行。

如果我使用 std::map,我会像这样实现它:

auto it = mymap.begin();
while(it != mymap.end())
{
delete it->second;
m.erase(it++);
}

TBB concurrent_hash_map的正确实现(和线程安全(是什么?

这是不可能的,因为 tbbconcurrent_hash_map不提供线程安全的迭代器。concurrent_unordered_map确实提供了线程安全的迭代器,但不幸的是,它不提供线程安全的擦除功能。

我只能向您推荐其他具有并发数据结构的库,例如libcds或我自己的xenium。