<thread>

公共成員函式
<thread>

std::thread::get_id

id get_id() const noexcept;
獲取執行緒 ID
返回執行緒 ID

如果 thread 物件是可連線的,則函式返回一個唯一標識該執行緒的值。

如果 thread 物件不可連線,則函式返回成員型別 thread::id 的一個預設構造的物件。

引數



返回值

成員型別 thread::id 的一個物件,該物件唯一標識該執行緒(如果可連線),或者(如果不可連線)預設構造。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// thread::get_id / this_thread::get_id
#include <iostream>       // std::cout
#include <thread>         // std::thread, std::thread::id, std::this_thread::get_id
#include <chrono>         // std::chrono::seconds
 
std::thread::id main_thread_id = std::this_thread::get_id();

void is_main_thread() {
  if ( main_thread_id == std::this_thread::get_id() )
    std::cout << "This is the main thread.\n";
  else
    std::cout << "This is not the main thread.\n";
}

int main() 
{
  is_main_thread();
  std::thread th (is_main_thread);
  th.join();
}

輸出
This is the main thread.
This is not the main thread.


資料競爭

該物件被訪問。

異常安全

無異常保證: 絕不丟擲異常。

另見