Thread get_id() function in C++ Last Updated : 16 Jun, 2021 Comments Improve Suggest changes 1 Likes Like Report Thread::get_id() is an in-built function in C++ std::thread. It is an observer function which means it observes a state and then returns the corresponding output. This function returns the value of std::thread::id thus identifying the thread associated with *this.Syntax: thread_name.get_id(); Parameters: This function does not accept any parameters.Return Value: This method returns a value of type std::thread::id identifying the thread associated with *this i.e. the thread which was used to call the get_id function is returned. The default constructed std::thread::id is returned when no such thread is identified.Below examples demonstrates the use of std::thread::get_id() method:Note: On the online IDE this program will show error. To compile this, use the flag “-pthread” on g++ compilers compilation with the help of command “g++ –std=c++14 -pthread file.cpp”. CPP // C++ program to demonstrate the use of // std::thread::get_id #include <chrono> #include <iostream> #include <thread> using namespace std; // util function for thread creation void sleepThread() { this_thread::sleep_for(chrono::seconds(1)); } int main() { // creating thread1 and thread2 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; } Possible Output: ID associated with thread1= 139858743162624 ID associated with thread2= 139858734769920 Create Quiz Comment K Kushagra7744 Follow 1 Improve K Kushagra7744 Follow 1 Improve Article Tags : C++ Programs Programming Language C++ CPP-Functions Processes & Threads +1 More Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++3 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++12 min readFile Handling in C++8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++3 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL2 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like