命名空间
变体
操作

std::this_thread::get_id

来自 cppreference.cn
< cpp‎ | thread
 
 
并发支持库
线程
(C++11)
(C++20)
this_thread 命名空间
get_id
(C++11)
(C++11)
(C++11)
协作式取消
互斥
(C++11)
通用锁管理
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
条件变量
(C++11)
信号量
闩锁和屏障
(C++20)
(C++20)
期值
(C++11)
(C++11)
(C++11)
(C++11)
安全回收
(C++26)
Hazard 指针
原子类型
(C++11)
(C++20)
原子类型的初始化
(C++11)(在 C++20 中弃用)
(C++11)(在 C++20 中弃用)
内存顺序
(C++11)(在 C++26 中弃用)
原子操作的自由函数
原子标志的自由函数
 
定义于头文件 <thread>
std::thread::id get_id() noexcept;
(C++11 起)

返回当前线程的 id

目录

[编辑] 参数

(无)

[编辑] 返回值

当前线程的 id

[编辑] 示例

#include <chrono>
#include <iostream>
#include <syncstream>
#include <thread>
using namespace std::chrono_literals;
 
void foo()
{
    std::thread::id this_id = std::this_thread::get_id();
 
    std::osyncstream(std::cout) << "thread " << this_id << " sleeping...\n";
 
    std::this_thread::sleep_for(500ms);
}
 
int main()
{
    std::jthread t1{foo};
    std::jthread t2{foo};
}

可能的输出

thread 140113018054400 sleeping...
thread 140113009661696 sleeping...

[编辑] 参见

返回线程的 id
(std::thread 的公共成员函数) [编辑]
C 文档,关于 thrd_current