如何使用boost :: thread :: id作为unordered_map的关键?

Ala*_*lan 3 c++ multithreading boost unordered-map

根据文档,a boost::thread::id可以被认为是每个正在运行的线程的唯一,并且可以在诸如std::set和的容器中使用std::map(因为<操作符被覆盖thread::id).

我的问题是我想thread::id用作一个键boost::unordered_map,但它要求键是"可以"(即支持散列到a size_t).由于thread :: id的所有实现细节都被隐藏了,所以我认为没有任何我可以使用的东西.

所以我的问题是 - 是否可以使用thread :: id作为unordered_map的键?

Mat*_* M. 5

您可以使用流媒体功能:

struct Hasher
{
  size_t operator()(const boost::thread::id& id)
  {
    std::ostringstream os; os << id; return hash(os.str());
  }
};
Run Code Online (Sandbox Code Playgroud)

课程的摘录很少,以便其他人可以看到可能的内容:

class thread::id
{
public:
    id();

    bool operator==(const id& y) const;
    bool operator!=(const id& y) const;
    bool operator<(const id& y) const;
    bool operator>(const id& y) const;
    bool operator<=(const id& y) const;
    bool operator>=(const id& y) const;

    template<class charT, class traits>
    friend std::basic_ostream<charT, traits>& 
    operator<<(std::basic_ostream<charT, traits>& os, const id& x);
};
Run Code Online (Sandbox Code Playgroud)