當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ Thread get_id用法及代碼示例


描述

它返回線程 ID。

聲明

以下是 std::thread::get_id 函數的聲明。

id get_id() const noexcept;

C++11

id get_id() const noexcept;

參數

返回值

它返回線程 ID。

異常

No-throw guarantee─ 從不拋出異常。

數據競爭

對象被訪問。

示例

在下麵的 std::thread::get_id 示例中。

#include <iostream>
#include <thread>
#include <chrono>

void foo() {
   std::this_thread::sleep_for(std::chrono::seconds(1));
}

int main() {
   std::thread sample(foo);
   std::thread::id sample_id = sample.get_id();

   std::thread sample2(foo);
   std::thread::id sample2_id = sample2.get_id();

   std::cout << "sample's id:" << sample_id << '\n';
   std::cout << "sample2's id:" << sample2_id << '\n';

   sample.join();
   sample2.join();
}

輸出應該是這樣的——

sample's id:139887397730048
sample2's id:139887389337344

相關用法


注:本文由純淨天空篩選整理自 C++ Thread Library - Function get_id。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。