Updated April 15, 2023
Introduction to C++ thread detach
In C++, thread detach is defined as a detaching of threads from its object without disturbing the execution, wherein other words, as the name, define the thread which has been declared using the detach() function will be separated or parted away from its own object by freeing up resources allocated by the thread before detach() function is called which means when the thread ends the execution or exits and also it the execution is continued independently. Such detached or separated threads are also called as background or demon threads. We should also note that we should not call the detach() function on the thread objects that are not executing or on the objects that are not related to the thread execution.
Working of thread detach() Function in C++ with Example
In this article, we will see a function known as detach() in C++ for detaching threads from its thread object without affecting its execution so that it continues alone without depending on this thread detaching process and this function simultaneously also frees up the thread resources related to it after thread exits. In general, we can say when a thread is created and suppose if we want to detach from the child thread, then if we run the detach() function on the parent thread for detaching the child thread, then there is no way that the parent thread can handle the child thread as the child thread has already left the parent thread so, therefore, it is a safety rule before using the detach() function we should think twice and also declare or make the child thread’s variable global or write these child’s variables in the global scope. But though we have detached, the thread does not get destroyed, but it still continues the execution.
Therefore if we want a parent thread to be detached from the child, then we can use this detach function. In C++, once the thread is detached or uses the detach() function, then we cannot stop such threads, and still, if there is a need for stopping such threads, then only one way is to return the thread from the initial thread function by instantiating it in main() function, adding the Boolean value but before exiting from the main() function we have to lock the mutex which means the Boolean value is set to true and later join the thread. So, therefore, this is not a straight or clean process to shut the detached thread, and this can be done only if the threads are joinable; therefore, it is better to avoid such shutting down of the detached threads.
There is a chance of exception occurrence, which the member function can throw by making the object of the thread to a valid state. So it is necessary to call this detach() function always for newly created threads to avoid program crash which sometimes is very difficult to resolve, and if this function is not called when program termination is called. The thread object can easily and safely be destroyed, along with which this thread object becomes non-joinable when a call is made to this function.
Now let us see how to write the detach() function or syntax in the C++ program.
It can be defined as:
std::thread::detach
void detach()
The above function does not take any arguments, nor does it return anything, but when we call detach *this, this function cannot have its own thread. There is a chance of an error to occur if the joinable() function is false, which is std::system:: error and hence make a child thread which we are detaching in the global scope.
Example:
Now let us see an example of demonstrating the detach() function using a C++ programming language.
Code:
#include <iostream>
#include <thread>
#include <chrono>
void thread_waiting(int n)
{
std::this_thread::sleep_for (std::chrono::seconds(n));
std::cout << "The main thread will pause for " << n << " seconds ended\n";
}
void detach_func()
{
std::cout << "It will freeup the resource and detaching 3 threads...\n";
std::thread (thread_waiting,1).detach();
std::thread (thread_waiting,2).detach();
std::thread (thread_waiting,3).detach();
std::cout << "Done with releasing of threads.\n";
}
void thread_call()
{
std::cout << "Starting the thread call and then apply detach function.\n";
detach_func();
std::cout << "Exiting the thread call after applying detach function.\n";
}
int main()
{
thread_call();
std::cout << "(the main thread will now pause for 5 seconds)\n";
thread_waiting(5);
return 0;
}
Output:
In the above program, we can see; first, we have to define header files, or libraries like <iostream> for input/ output and <thread> library as we are dealing with threads, and <chrono> library is defined for declaring date and time. And we are then defining a function for making the main thread to pause for few seconds, and here we cannot directly declare time or the <chrono> library we have to use or define it under std::chrono:: namespace.
We have to create or call threads so that we can apply the detach function on the threads created. Then we will define a function where we are detaching the threads by applying the detach() function. This detaching action is done separated in the detach_func() where it will free up the resources and detach the 3 threads as we have defined for only 3 threads, so one by one thread is detached it will give that is done with releasing of the threads which means we are separating the threads from parent threads. Then the thread exits, but here the main thread will wait for 5 seconds, as shown in the above screenshot.
Conclusion
In this article, we conclude that in C++, we have detach() function in <thread> library used for detaching the threads from its thread object where it will free up the allocated resources when the threads have completed the execution; that is, it can run the thread execution independently and hence these detached threads are also called as background or demon threads. In this article, we also saw how to define and declare the detach() function, and we also have demonstrated a sample example where we are calling a thread and applying the detach() function, and we can see that how the main thread pauses for few seconds after exiting the thread execution.
Recommended Articles
This is a guide to C++ thread detach. Here we discuss the introduction and working of thread detach() function in C++ with example. You may also have a look at the following articles to learn more –