std::hash<std::thread::id>

定义于头文件 <thread>
template<> struct hash<std::thread::id>;
(C++11 起)

std::hashstd::thread::id 类的模板特化允许用户获得线程标识符的哈希。

示例

#include <iostream>
#include <thread>
#include <chrono>
#include <vector>
using namespace std::chrono_literals;
void foo()
{
    std::this_thread::sleep_for(10ms);
}
 
int main()
{
    std::vector<std::thread> v;
    for(int n = 0; n < 4; ++n)
        v.emplace_back(foo);
 
    std::hash<std::thread::id> hasher;
    for(auto& t : v) {
        std::cout << "thread " << t.get_id() << " hashes to " << hasher(t.get_id()) << '\n';
        t.join();
    }
}

可能的输出:

thread 139786440144640 hashes to 8905351942358389397
thread 139786431751936 hashes to 9222844670065909738
thread 139786423359232 hashes to 18199000599186780501
thread 139786414966528 hashes to 15386662774029264672

参阅

(C++11)
哈希函数对象
(类模板)

版本历史

  • (当前 | 先前 2017年8月24日 (四) 04:58Fruderica讨论 | 贡献. . (1,194字节) (+301). . (撤销)
  • 当前 | 先前 2013年7月2日 (二) 11:35P12bot讨论 | 贡献 . . (893字节) (-65). . (Use {{lc}}. Update links. Various fixes.) (撤销)
  • 当前 | 先前 2012年11月2日 (五) 18:28P12bot讨论 | 贡献 . . (958字节) (+265). . (r2.7.3) (机器人添加:de, en, es, fr, it, ja, pt, ru) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 03:32P12讨论 | 贡献 . . (693字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前) 2012年10月25日 (四) 12:00TranslationBot讨论 | 贡献. . (693字节) (+693). . (Translated from the English version using Google Translate)