Does C++ 11 thread automatically destroy after detach

帅比萌擦擦* 提交于 2021-02-07 05:54:08

问题


Normally, I would assume that C++ 11 thread automatically destroy after detach. But the thing is, I can't find anything to prove this assumption.

According to this article

Once detached, the thread should live that way forever.

forever? If the function of the thread finish, Does its resource remain forever?

According to this article

After a call to this function, the thread object becomes non-joinable and can be destroyed safely.

It can be destroyed safely, but is it automatically?

If it's not destroyed automatically, how to destroy it (not forcefully, but after the function of the thread end)

Thanks for reading.


回答1:


You should consult a better reference. From std::thread::detach:

Separates the thread of execution from the thread object, allowing execution to continue independently. Any allocated resources will be freed once the thread exits.

After calling detach *this no longer owns any thread.

So to answer your questions (if they aren't already):

forever?

No. If the thread finishes (for example: if it counts to 10), it is done and it is not running anymore.

Does its resource remain forever?

No, when the thread finishes, every resource from the thread is freed (like variables and such).

It can be destroyed safely, but is it automatically?

What do you mean? When the thread is done, it is destroyed (automatically), regardless of whether you call detach or not. The only difference is that here, they are referring to the thread object, so the actual std::thread instance.

So when the thread object is destructed, you must have called join or detach regardless whether you own that the actual thread finished or not. If you don't, std::terminate is called.




回答2:


A std::thread is just a thin wrapper around your platform's native threading library... It doesn't really contain much internally besides a few flags and the native handle to the platform's thread representation (which for both Unix & Windows is a fancy integral identifier). Speaking specifically of Unix-like systems, the call to std::thread::detach() does two things:

  • calls some variation of pthread_detach(), which frees a data structure used to store the returning void pointer from platform-native thread-main.
  • sets a flag in the class denoting that the thread has been detached, and that, in turn, prevents the destructor from throwing an exception.

As for any other resources that the thread creation may set up, those should be cleaned up by your platform's run-time when your thread-main exits.

So, putting it another way, std::thread::detach just allows the wrapper to be destroyed without throwing an exception; the actual thread's stack and OS resource reclamation happens in the thread's execution context after your thread's main function exits, and that should be 100% automatic.



来源:https://stackoverflow.com/questions/45685463/does-c-11-thread-automatically-destroy-after-detach

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!