A thread in C++ represents an independent sequence of execution that allows multiple tasks to run concurrently. When a thread does not need to synchronize with the thread that created it, it can be detached to execute independently.
- std::thread::detach() separates a thread from its associated thread object.
- A detached thread runs independently and releases its resources automatically after completion.
#include <iostream>
#include <thread>
using namespace std;
void task()
{
cout << "Detached thread is running\n";
}
int main()
{
thread t(task);
t.detach();
cout << "Main thread continues execution\n";
return 0;
}
Output
Main thread continues execution Detached thread is running
Explanation: After calling detach(), the worker thread executes independently while the main thread continues without waiting for it.
std::thread::detach()
The detach() member function separates a thread from its std::thread object. After detaching, the thread continues executing in the background, and its resources are automatically released when execution completes.
Syntax
void detach();
- Parameters: This function does not accept any parameters.
- Return Value: It does not return any value.
Working of std::thread::detach()
After a thread is detached, it is no longer managed by the corresponding std::thread object. The detached thread continues executing independently until it finishes. The execution flow is as follows:
- Create a thread to execute a task.
- Call detach() on the thread object.
- The thread starts executing independently.
- The calling thread continues execution immediately.
- The detached thread releases its resources automatically after completion.
#include <chrono>
#include <iostream>
#include <thread>
using namespace std;
void threadFunction()
{
cout << "Detached thread executing...\n";
this_thread::sleep_for(chrono::seconds(2));
cout << "Detached thread completed.\n";
}
int main()
{
thread detachedThread(threadFunction);
detachedThread.detach();
cout << "Main thread continuing...\n";
// Prevent program from terminating immediately
this_thread::sleep_for(chrono::seconds(3));
return 0;
}
Output
Main thread continuing... Detached thread executing... Detached thread completed.
Explanation
- A new thread is created and detached using detach(), allowing it to execute independently.
- The main thread continues execution immediately, while the detached thread completes its task and automatically releases its resources.
Rules for Using std::thread::detach()
While using detach(), keep the following rules in mind:
- A detached thread cannot be joined later.
- After detaching, the thread object becomes non-joinable.
- The thread continues executing even after the thread object is destroyed.
std::thread::joinable() After detach()
Once a thread is detached, it becomes non-joinable.
#include <iostream>
#include <thread>
using namespace std;
void task() {}
int main()
{
thread t(task);
t.detach();
if (t.joinable())
cout << "Thread is joinable\n";
else
cout << "Thread is not joinable\n";
return 0;
}
Output
Thread is not joinable
Explanation: Calling detach() transfers ownership of the thread to the operating system, so the associated std::thread object can no longer join it.
Advantages of detach()
Using detach() provides several benefits:
- Allows the calling thread to continue without waiting.
- Suitable for long-running background tasks.
- Automatically releases thread resources after completion.
- Simplifies execution when synchronization is not required.
Limitations of detach()
Despite its usefulness, detach() has a few limitations:
- Detached threads cannot be synchronized using join().
- The return value or completion status of the thread cannot be obtained.
- Shared resources must remain valid until the detached thread finishes.
When Should You Use detach()?
detach() is useful in situations where:
- The thread performs independent background work.
- The calling thread does not need the thread's result.
- Synchronization between threads is unnecessary.
Related article: Difference between remove() and detach() Methods