Main idea of this section to discuss various use cases of detach(), d's and don'ts of detach function.
Let check followings open items. if you have some question then you can put them in comment section.
Question-1: What will happen if main thread exits before detached thread finished its assigned task?
Question-2: Can we rejoin a detached thread?
Question-1: What will happen if main thread exits before detached thread finished its assigned task?
Detach thread shall be stopped there itself and all acquired resources shall be freed. Let check with following example.
[Copy, Paste & Run]
#include<iostream> |
$ ./detach Performing Activity in main() www.techsujhav.com :: 0 Performing Activity in main() www.techsujhav.com :: 1Performing Activity in main() www.techsujhav.com :: 2 |
Explanation: As it is clearly visible that www.techsujhav.com :: 3 and www.techsujhav.com :: 4 were not printed because main exited before our displayActivity thread could finish its activity. |
Question-2: Can we rejoin a detached thread?
No it is not possible to join a thread once we have
called detach() function on that object. It will throw an exception.
As we know if we want to call join() on a
thread-object object then that thread object must be ACTIVE (joinable() ==
true).
Let check with example
#include<iostream> #include<thread> #include<chrono> using namespace std; void displayActivity() { for(int i=0;i<5;i++) { cout<<"www.techsujhav.com :: "<<i<<endl; std::this_thread::sleep_for(1ns); } } int main() { {//new scope std::thread t1(displayActivity); cout<<"Going to call detach()"<<endl; t1.detach(); try { cout<<"Going to call join()"<<endl; t1.join(); } catch(std::exception& e) { cout<<"Exception Caught :: "<<e.what()<<endl; } } return 0; } |
$./detach Going to call detach() Going to call join() Exception Caught :: Invalid argument www.techsujhav.com :: 0 |
PREV:: std::thread::detach()
Your Comments /Suggestions & Questions are always welcome.
We would like to help you with best of our knowledge.
So feel free to put Questions.
what you gave is confusing, because once a thread is detached , it will run independently from main function .
ReplyDeleteThe detached thread t1 starts executing displayActivity and prints "www.techsujhav.com :: 0".
The main thread enters the loop in main, prints "Performing Activity in main()", and sleeps for 1 nanosecond (which is very short and might not have a noticeable effect).
The detached thread t1 continues running and prints "www.techsujhav.com :: 1", "www.techsujhav.com :: 2".
The main thread goes through two more iterations of the loop, printing "Performing Activity in main()" each time.
The detached thread t1 continues and prints "www.techsujhav.com :: 3", "www.techsujhav.com :: 4".
Keep in mind that the sleep duration of 1 nanosecond is extremely short, and in practice, the operating system's scheduler may not have enough granularity to make the interleaving very noticeable. You might consider using a longer sleep duration for a more visible effect.
Hi Pavan
DeleteThanks for making an effort to post your inputs. Will improve on your suggestions.
Happy to help you again.
TechSujhav