问题描述
如果线程被异步取消,自动对象的析构函数是否保证执行?
Are destructors for automatic objects guaranteed to execute if a thread is cancelled asynchronously?
推荐答案
取消一个线程最终会调用 pthread_exit()
就我可以告诉从googling将调用析构函数。它通过抛出一些保证未捕获异常到线程包装器来实现这一点,所以所有基于堆栈的对象以正确的顺序被破坏。
Cancelling a thread eventually ends up invoking pthread_exit()
, which as far as I can tell from googling will invoke destructors. It does this by throwing some kind of 'guaranteed uncaught' exception all the way out to the thread wrapper, so all your stack-based objects get destructed in the correct order.
请参见,例如。和:
要做到这一点pthread_exit b $ b模糊异常并且在删除线程之前捕获它
。这样
它清理所有对象很好。在
另一方面,捕获...变成
不可能。
To do that pthread_exit() throws some obscure exception and catches it right before ditching the thread. This way it cleans up all objects nicely. On the other hand, catching … becomes impossible.
这篇关于C ++析构函数和PTHREAD_CANCEL_ASYNCHRONOUS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!