How to Detach 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 detach a thread in C++. What does Detaching a Thread mean?Detaching a thread means allowing the thread to execute independently from the thread that created it. Once detached, the parent thread can continue its execution without waiting for the detached thread to finish. Detaching a thread is useful when you don't need to synchronize with the thread or obtain its return value. How to Detach a Thread in C++?In C++, you can detach a thread by calling the detach() member function on an std::thread object. Once detached, the thread's resources will be automatically released when it completes its execution. Note: You should be careful while detaching a thread as the main thread may terminate before the thread completes its task. C++ Program to Detach a Thread C++ // C++ Program to illustrate how to detach a thread #include <chrono> #include <iostream> #include <thread> using namespace std; void threadFunction() { // Some work cout << "Detached thread executing..." << endl; this_thread::sleep_for( chrono::seconds(2)); // Simulate some work cout << "Detached thread completed." << endl; } // Driver Code int main() { thread detachedThread(threadFunction); // Detach the thread detachedThread.detach(); // Main thread continues execution without waiting for // detachedThread cout << "Main thread continuing..." << endl; // Give some time for detached thread to complete this_thread::sleep_for(chrono::seconds(3)); return 0; } Output: Main thread continuing...Detached thread executing...Detached thread completed.In this example, the detachedThread is created and detached using the detach() function. After detaching the thread, the main thread continues its execution without waiting for detachedThread to finish. The detached thread will execute independently and complete its work, printing the messages "Detached thread executing..." and "Detached thread completed." before the program terminates. Comment More infoAdvertise with us Next Article How to Detach a Thread in C++? H harshsingh123 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 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 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 How to Create a shared_ptr in C++? A std::shared_pointer is a smart pointer introduced in C++11 that manages the lifetime of a dynamically allocated object through reference counting. In this article, we will learn how to create a shared_pointer. shared_ptr in C++A std::shared_pointer can be created by using the std::make_shared meth 2 min read How to Delete a Pointer in C++? In C++, memory management is essential to avoid memory leaks, crashes, and undefinable behavior. In this article, we will learn how to properly delete a pointer in C++. Deleting a Pointer in C++By deleting a pointer, we refer to deleting the memory allocated to the pointer. To delete a pointer in C+ 2 min read Like