Thread get_id() function in C++ Last Updated : 16 Jun, 2021 Summarize Comments Improve Suggest changes Share Like Article 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 Comment More infoAdvertise with us Next Article std::thread::join() in C++ K Kushagra7744 Follow Improve Article Tags : C++ Programs Programming Language C++ CPP-Functions Processes & Threads +1 More Practice Tags : CPP Similar Reads Thread joinable() function in C++ Thread::joinable 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 and checks whether the thread object is joinable or not. A thread object is said to be joinable if it identifies/represent an active threa 2 min read std::thread::join() in C++ The std::thread::join() is a standard library function in C++ that is used to block the current thread until the thread identified by *this finishes its execution. It means that it will hold the current thread at the point of its call until the thread associated with that join() function finishes it 3 min read How to Create a Thread in C++? A thread is a basic element of multithreading which represents the smallest sequence of instructions that can be executed independently by the CPU. In this article, we will discuss how to create a thread in C++. How to Create a Thread in C++?In C++, the std::thread is a class template that is used t 2 min read How to Detach a Thread in C++? In C++, a thread is a basic element of multithreading that represents the smallest sequence of instructions that can be executed independently by the CPU. In this article, we will discuss how to detach a thread in C++. What does Detaching a Thread mean?Detaching a thread means allowing the thread to 2 min read How to Join a Thread in C++? In C++, a thread is a basic element of multithreading that represents the smallest sequence of instructions that can be executed independently by the CPU. In this article, we will discuss how to join a thread in C++. How to Join a Thread in C++?Joining a thread is a means to wait for the thread to c 2 min read Function Pointer to Member Function in C++ In C++, function pointers enable users to treat functions as objects. They provide a way to pass functions as arguments to other functions. A function pointer to a member function is a pointer that points to a non-static member function of a class. In this article, we will learn how to use a functio 3 min read Like