How to Join a Thread in C++? Last Updated : 20 Mar, 2024 Comments Improve Suggest changes Like Article Like Report 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 complete its execution before moving to the next part of the program. We can join the thread using the std::thread::join() function. It is a member function that makes sure that the execution of the thread is complete before moving on to the next statement after the join() function call. C++ Program to Join a Thread C++ // C++ Program to illustrate how to join a thread #include <iostream> #include <thread> using namespace std; // Define a function to be executed by the thread void myFunction() { cout << "Thread started" << endl; // Do some work cout << "Thread finished" << endl; } int main() { // Print a message indicating the start of the main // thread cout << "Main thread started" << endl; // Create a new thread and associate it with the // function myFunction thread myThread(myFunction); // Wait for the created thread (myThread) to finish // execution myThread.join(); cout << "Main thread finished" << endl; return 0; } Output:Main thread startedThread startedThread finishedMain thread finished Comment More infoAdvertise with us Next Article How to Join a Thread in C++? R rohitdubey214 Follow Improve Article Tags : C++ Programs C++ cpp-multithreading CPP Examples Practice Tags : CPP Similar Reads 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 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 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 How to Add Timed Delay in C++? In C++, there is the functionality of delay or inactive state which allows the execution to be delayed for a specific period of time. This is often referred to as a âtimed delayâ. In this article, we will learn how to add timed delay in C++. Timed Delay in C++To add a timed delay in C++, we can use 2 min read Like