Given below:
void test()
{
std::chrono::seconds dura( 20 );
std::this_thread::sleep_for( dura );
}
int main()
{
std::thread th1(test);
std::chrono::seconds dura( 5 );
std::this_thread::sleep_for( dura );
return 0;
}
main
will exit after 5 seconds, what will happen to th1
that's still executing?
Does it continue executing until completion even if the th1
thread object you defined in main
goes out of scope and gets destroyed?
Does th1
simply sits there after it's finished executing or somehow gets cleaned up when the program terminates?
What if the thread was created in a function, not main
- does the thread stays around until the program terminates or when the function goes out of scope?
Is it safe to simply not call join
for a thread if you want some type of timeout behavior on the thread?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…