Tach
Tach

Reputation: 11

How to use weak_ptr in tbb::concurrent_unordered_map?

I am using tbb::concurrent_unordered_map to replace std::map in my program like this:

Before:

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

Now, I use tbb::concurrent_unordered_map to replace std::map , but it has some compile errors:

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: error: invalid static_cast from type 'const std::weak_ptr' to type 'std::size_t
{aka long unsigned int}'
return static_cast( t ) * internal::hash_multiplier;

I have try some solutions like this , but it does not work:

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);
}

So, how can it work, please help....

Upvotes: 1

Views: 268

Answers (1)

Alex
Alex

Reputation: 632

You need to define a hash function for std::weak_ptr. You can find the example in tests for the TBB library.

Upvotes: 1

Related Questions