名稱空間
變體
操作

std::jthread::get_id

來自 cppreference.com
< cpp‎ | thread‎ | jthread
 
 
併發支援庫
執行緒
(C++11)
(C++20)
this_thread 名稱空間
(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)
危險指標
原子型別
(C++11)
(C++20)
原子型別的初始化
(C++11)(C++20 中已棄用)
(C++11)(C++20 中已棄用)
記憶體排序
(C++11)(C++26 中已棄用)
原子操作的自由函式
原子標誌的自由函式
 
 
std::jthread::id get_id() const noexcept;
(C++20 起)

返回一個 std::jthread::id 型別的值(它是 std::thread::id 的類型別名),用於標識與 *this 關聯的執行緒。

目錄

[編輯] 引數

(無)

[編輯] 返回值

一個 std::jthread::id 型別的值,用於標識與 *this 關聯的執行緒。如果沒有關聯的執行緒,則返回預設構造的 std::jthread::id

[編輯] 示例

#include <chrono>
#include <iostream>
#include <thread>
 
void foo()
{
    std::this_thread::sleep_for(std::chrono::seconds(1));
}
 
int main()
{
    std::jthread t1(foo);
    std::jthread::id t1_id = t1.get_id();
 
    std::jthread t2(foo);
    std::jthread::id t2_id = t2.get_id();
 
    std::cout << "t1's id: " << t1_id << '\n';
    std::cout << "t2's id: " << t2_id << '\n';
 
    t1.join();
    t2.join();
 
    std::cout << "t1's id after join: " << t1.get_id() << '\n';
    std::cout << "t2's id after join: " << t2.get_id() << '\n';
}

可能的輸出

t1's id: 140146221688576
t2's id: 140146213295872
t1's id after join: thread::id of a non-executing thread
t2's id after join: thread::id of a non-executing thread

[編輯] 參閱

表示執行緒的 id
(std::thread 的公共成員類) [編輯]
檢查執行緒是否可join,即是否可能在並行上下文中執行
(公共成員函式) [編輯]