이 튜토리얼에서는 C++에서 스레드 get_id() 함수를 이해하는 프로그램에 대해 논의할 것입니다.
Thread get_id() 함수는 프로세스의 현재 상태를 확인한 다음 실행 중인 현재 스레드의 ID를 반환합니다. 이 함수는 매개변수를 사용하지 않습니다.
예시
#include <chrono>
#include <iostream>
#include <thread>
using namespace std;
//creating thread
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;
} 출력
ID associated with thread1= 135456142132844 ID associated with thread2= 135121414221716