I am using C++11 std::thread. My confusion is based on the following:
terminate()
on a thread is called if neither join()
nor detach()
is called on it. That means when a thread returns:
a. it checks whether a join()
or detach()
on it is called, if not, then terminate()
is called(produces a coredump),
b. if join()
is called the destructor of the thread would be called by main(or calling thread)
c. if a detach()
was called while the thread was executing, the thread won't return to main for cleanup, instead this thread itself will be responsible for calling its own destructor
Question: If the above points were true then i dont understand at which exact point is the decision made wehter to terminate a thread or not based on whether a join()
or detach()
is called or not?
void function_1()
{
return;
}
int main()
{
std::thread t1(function_1);
std::cout<<"\nFirst Sleep for 5 seconds:\n";
sleep(5);
cout<<"\n2nd Sleep for 5 seconds:\n";
sleep(5);
if(t1.joinable())
{
cout<<"\nGonna join()\n";
t1.detach();
}
cout<<"Exiting from main ...\n";
}
Since the thread would finish its execution before 5th second, i assumed the thread should be terminated as there is no join()/detach()
till this point. But there's no terminate()
called. Instead a thread is detached succesfully.
However if i dont detach then terminate is called after 2 sleep after "Exiting from main...."
, right before return 0;
Does that mean that the thread is not cleaned up(its destructor not called), until main thread(or called thread) returns?
Is that the reason why one should detach()
so that the cleanup of the thread doesn't have to wait till main(or caller thread) returns?
Aucun commentaire:
Enregistrer un commentaire