void thread_fn() {
while (1) {
this_thread::sleep_for(chrono::seconds(1));
std::cout << "Inside thread function\n";
}
}
int main()
{
cout << getpid() << "My PID" << endl;
std::thread t1(thread_fn);
t1.detach();
return 0;
}
QUESTION 1 -What will happen to detached thread, after main exits? After some reading i figured out OS will try to do the cleanup, once thread exits. But here i am not exitting at all. Will it keep on running for ever? (till system reboots..)
QUESTION 2 -How can i kill it when process which invoked the thread has already been killed?
QUESTION 3 -Is there a need to kill it (assume thread is doing some heavy task)?