简体   繁体   中英

Situation of detached std::thread after parrent thread left its block

I've searched but i couldnt find exact answer of this question. For instance, if we have a code block like this:

{
  std::thread thread_1(.....);
  thread_1.detach();
  ...
}

when thread_1 still works, what happens exactly after parent thread left this code block.

Case 1: I am running this on a very bad operating system, or maybe very primitive, then as you have detached the thread from main thread, so it works in a independently of the main thread. So even though the main thread exits, it will keep on running as long as it is not dependent on any variables present at global scope, it will be stray thread.

Case 2: This is running on a modern opertaing system, we can say that it will behave as same as that in case 1 , but modern OS on seeing a widowed thread will immediately kill it and clean it, releasing the resources captured by it.

hope this is helpful, if incorrect please suggest corrections in the comments below...thankyou

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM