我想在地图中存储我自己的一个类,其键的类型为pthread_t。(我的类的每个对象都包含一个pthread_t数据成员,因此我希望每个实例都与该pthread_t关联作为键,在地图上) 问题是pthread_t无法与pthread_equal进行比较,因此我无法将其作为键放在地图中。
我见过unordered_map,但是如何为pthread_t实现哈希函数?
到现在为止我想过使用pthread_self()来比较它们,但由于返回的值是pthread_t,这也是不可能的,我不想使用这只是unsigned的typedef的事实long int。
如果无法使用某种地图,我如何将pthread_t的对象存储在容器中并使用pthread_self()函数快速找到它们?
答案 0 :(得分:1)
这种方式应该可以工作:
bool myCmp(const pthread_t &a, const pthread_t &b)
{
return memcmp(&a, &b, sizeof(pthread_t)) < 0;
}
map<pthread_t, myDataType, myCmp> myMap;