在本教程中,我们将讨论一个程序来理解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