I started to use the unordered_set class from the tr1 namespace to speed-up access against the plain (tree-based) STL map. However, I wanted to store references to threads ID in boost (boost::thread::id), and realized that the API of those identifiers is so opaque that you cannot clearly obtain a hash of it.
Surprisingly, boost implements parts of the tr1 (including hash and unordered_set), but it does not define a hash class that is able to hash a thread ID.
Looking at the documentation of boost::thread::id I found that thread IDs can be output to a stream, so my solution for doing hashing was kind of:
struct boost_thread_id_hash
{
size_t operator()(boost::thread::id const& id) const
{
std::stringstream ostr;
ostr << id;
std::tr1::hash<std::string> h;
return h(ostr.str());
}
};
That is, serialize it, apply the hash to the resulting string. However, this seems to be less efficient than actually using the STL map<boost::thread::id>.
So, my questions: Do you find a better way of doing this? Is it a clear inconsistency in both boost and tr1 not to force the existence of a hash<boost::thread::id> class?
Thanks.
The overhead of stringifying
thread::id(only to compute the string hash afterward) is, as you almost said yourself, astronomical compared to any performance benefits atr1::unordered_mapmight confer vis-a-visstd::map. So the short answer would be: stick with std::map< thread::id, … >If you absolutely must use unordered containers, try to use
native_handle_typeinstead ofthread::idif possible, i.e. prefertr1::unordered_map< thread::native_handle_type, ... >, invokingthread::native_handle()instead ofthread::get_id()wheninserting andfinding.DO NOT attempt anything like the following:
It could work, but is extremely brittle and an almost guaranteed timebomb. It assumes intimate knowledge of the inner workings of the
thread::idimplementation. It will get you cursed at by other developers. Don’t do it if maintainability is of any concern! Even patchingboost/thread/detail/thread.hppto addsize_t hash_value(const id& tid)as a friend ofthread::idis “better”. 🙂