概要
関連付けられているスレッドのスレッド識別子を取得する。
戻り値
threadオブジェクトがスレッドに関連付けられていない場合はデフォルト構築されたidオブジェクト、そうでなければ*thisが関連付けられているスレッドを表すstd::this_thread::get_id()を返す。
例外
送出しない。
例
#include <thread>
#include <cassert>
#include <iostream>
int main()
{
std::jthread jt1;
assert( jt1.get_id() == std::jthread::id{} );
std::jthread jt2{ []{ /*...*/ } };
assert( jt2.get_id() != std::jthread::id{} );
std::cout << jt1.get_id() << std::endl;
std::cout << jt2.get_id() << std::endl;
}
出力例
thread::id of a non-executing thread
139880063837952
バージョン
言語
- C++20
処理系
- Clang:
- GCC: 10.2.0 ✅
- Visual C++: ??
参照
- P2422R1 Remove
nodiscardannotations from the standard library specification- C++26で
[[nodiscard]]指定が削除された
- C++26で