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

发布于 2024-09-01 11:49:29 字数 583 浏览 2 评论 0原文

根据文档,一个 boost::thread::id 可以被认为对于每个正在运行的线程是唯一的,并且可以在 std::setstd 等容器中使用::map (因为 < 运算符被 thread::id 覆盖)。

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

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

According to the documentation, a boost::thread::id can be considered unique for each running thread and can be used in containers such as std::set and std::map (because the < operator is overridden for thread::id).

My problem is that I'd like to use thread::id as a key to an boost::unordered_map, however it requires that the key is "hashable" (ie. supports hashing to a size_t). Since all implementation detail for thread::id is hidden I don't think have anything I can use.

So my question is - is it possible to use thread::id as a key to an unordered_map?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

作业与我同在 2024-09-08 11:49:29

您可以使用流功能:

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

该类的小摘录,以便其他人可以看到什么是可能的:

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

You can use the streaming ability:

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

Little excerpt of the class, so that others may see what's possible:

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);
};
最冷一天 2024-09-08 11:49:29

你有多少个线程?除非你有超过几百个,否则不太可能
具有大量散列的 unordered_map (散列非常重,尤其是基于 std::stringstream)会比 std::map 更快。不要忘记 std::map 具有相当小的常数的日志复杂性。

如果您有数百个线程,那么您的应用程序可能存在问题。

How many threads do you have? Unless you have more then several hundred it is unlikely that
unordered_map with heavy hash (and hash is heavy especially based on std::stringstream) would be faster then std::map. Don't forger that std::map has log complexity with quite small constant.

And if you have hundreds of threads, then there is probably a problem with your application.

怀里藏娇 2024-09-08 11:49:29

为什么需要继续处理字符串?您可以使用

size_t operator()(const boost::thread::id& id)
{   
    using boost::hash_value;

    return hash_value(id);
}

Why would you need to proceed with strings? You can use

size_t operator()(const boost::thread::id& id)
{   
    using boost::hash_value;

    return hash_value(id);
}
鯉魚旗 2024-09-08 11:49:29

文档说它可以写入流。将其写入 std::ostringstream 并对 str() 结果进行哈希处理。尽管输出格式未指定,但它对于给定 ID 来说是唯一的,并且对于程序的给定运行而言是一致的(只要线程 ID 仍然有效)。

The documentation says it can be written to a stream. Write it to a std::ostringstream and hash the str() result. Although the output format is unspecified, it's unique for a given ID and consistent for a given run of your program (which is as long as the thread ID would remain valid anyway).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文