Skip to content

Latest commit

 

History

History
66 lines (49 loc) · 1.63 KB

File metadata and controls

66 lines (49 loc) · 1.63 KB

get_id

  • thread[meta header]
  • std[meta namespace]
  • jthread[meta class]
  • function[meta id-type]
  • cpp20[meta cpp]
[[nodiscard]] id get_id() const noexcept; // (1) C++20
id get_id() const noexcept;               // (1) C++26
  • id[link /reference/thread/thread/id.md]

概要

関連付けられているスレッドのスレッド識別子を取得する。

戻り値

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;
}
  • get_id()[color ff0000]
  • std::jthread::id[link /reference/thread/thread/id.md]

出力例

thread::id of a non-executing thread
139880063837952

バージョン

言語

  • C++20

処理系

参照