当主要退出时,分离线程会发生什么?

时间:2017-06-30 06:47:11

标签: c++ multithreading

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;
}

问题1 - 在主要退出后,分离线程会发生什么?经过一些阅读后我发现OS会在线程退出后尝试进行清理。但在这里我根本不会放弃。它会一直运行吗? (直到系统重新启动..)

问题2 - 当调用线程的进程已经被杀死时,我怎么能杀掉它?

问题3 - 是否需要杀死它(假设线程正在执行一些繁重的任务)?

0 个答案:

没有答案
相关问题