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

来自cppreference.com
< cpp‎ | thread‎ | thread‎ | id
 
 
线程支持库
线程
(C++11)
this_thread 命名空间
(C++11)
(C++11)
(C++11)
互斥
(C++11)
通用锁管理
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
条件变量
(C++11)
期货
(C++11)
(C++11)
(C++11)
(C++11)
 
 
std::thread::id
成员函数
非成员函数
帮助类
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)
哈希函数对象
(类模板) [编辑]