如何在 tbb::concurrent_unordered_map 中使用weak_ptr

How to use weak_ptr in tbb::concurrent_unordered_map?

本文关键字:map weak ptr unordered tbb concurrent      更新时间:2023-10-16

我正在使用tbb::concurrent_unordered_map来替换程序中的std::map,如下所示:

Before:
class KvSubTable;
typedef std::weak_ptr<KvSubTable> KvSubTableId;
std::map<KvSubTableId, int, std::owner_less<KvSubTableId> > mEntryMap;

现在,我使用tbb::concurrent_unordered_map来替换std::map,但它有一些编译错误:

tbb::concurrent_unordered_map<KvSubTableId, int, tbb::tbb_hash<KvSubTableId>, std::owner_less<KvSubTableId> > mEntryMap;

CPP/EXT/AMD64/include/tbb/internal/_tbb_hash_compare_impl.h:66:37: 错误:类型"const "中的static_cast无效 std::weak_ptr"以键入"std::size_t
{又名长无符号 int}'
返回 static_cast( t ( * 内部::hash_multiplier;

我已经尝试了一些这样的解决方案,但它不起作用:

template <typename T>
inline bool operator==(const std::weak_ptr<T>& t, const std::weak_ptr<T>& u)
{
    return !t.owner_before(u) && !u.owner_before(t);
}

那么,它如何工作,请帮忙。

你需要为std::weak_ptr定义一个哈希函数。您可以在 TBB 库的测试中找到该示例。