get_id () function return a unique value that is associated with a give thread called as thread-id.
std::thread::id is the value returned by get_id ()function
Now following program shall help you understand it’s usage.
#include <iostream> #include<thread> //Thread Header using namespace std; //displayAcitviy- is the actual function that is being //called by the thread. void displayAcitviy() { cout<<__func__<<"::"<<__LINE__<<" -->t1-Thread-ID= "<<std::this_thread::get_id(); cout<<" www.techsujhav.com "<<endl; } int main() { cout<<__func__<<"::"<<__LINE__<<" -->MAIN-Thread-ID= "<<std::this_thread::get_id(); cout<<" ::Going to create thread "<<endl; //Create thread std::thread t1(displayAcitviy); cout<<__func__<<"::"<<__LINE__<<" -->t1-Thread-ID= "<<t1.get_id()<<endl; //join()- wait for thread t1 to complete its activity t1.join(); //exit Main cout<<__func__<<"::"<<__LINE__<<" -->MAIN-Thread-ID::"<<std::this_thread::get_id(); cout<<" ::Exit"<<endl; return 0; } |
Compile: g++ threadId.cpp -o
threadId -lpthread |
Output: main::19 -->t1-Thread-ID= 139984804210432 displayAcitviy::9 -->t1-Thread-ID= 139984804210432
www.techsujhav.com main::24 -->MAIN-Thread-ID::139984822441792 ::Exit |
Your Comments /Suggestions & Questions are always welcome.
We would like to help you with best of our knowledge.
So feel free to put Questions.
So feel free to put Questions.
No comments:
Post a Comment