std::jthread::get_id
來自 cppreference.com
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,即是否可能在並行上下文中執行 (公共成員函式) |