इस ट्यूटोरियल में, हम C++ में थ्रेड get_id() फंक्शन को समझने के लिए एक प्रोग्राम पर चर्चा करेंगे।
थ्रेड get_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