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<<" "<<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

//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

main::15 -->MAIN-Thread-ID= 139984822441792 ::Going to create thread

main::19 -->t1-Thread-ID= 139984804210432

displayAcitviy::9 -->t1-Thread-ID= 139984804210432

main::24 -->MAIN-Thread-ID::139984822441792 ::Exit

How to check and match std::thread::id with thread-id shown in gdb (debugger). [Click Here]

Your Comments /Suggestions & Questions are always welcome. 
We would like to help you with best of our knowledge.
So feel free to put Questions.

No comments:

Post a Comment