Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C++

Hàm thread get_id() trong C++

thread::get_id() trả về ID định danh duy nhất của luồng hiện tại. Không tham số.

Ví dụ

#include <chrono>
#include <iostream>
#include <thread>
using namespace std;

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

int main() {
    thread thread1(sleepThread);
    thread thread2(sleepThread);
    thread::id t1_id = thread1.get_id();
    thread::id t2_id = thread2.get_id();
    cout << "ID associated with thread1= " << t1_id << endl;
    cout << "ID associated with thread2= " << t2_id << endl;
    thread1.join(); thread2.join();
    return 0;
}

Đầu ra

ID associated with thread1= 135456142132844
ID associated with thread2= 135121414221716