本文介绍了当我在C ++中杀了一个pThread,堆栈上的对象的析构函数是否被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个多线程的C ++程序。我计划杀死线程。然而,我也使用ref计数GC。

I'm writing a multi-threaded C++ program. I plan on killing threads. However, I am also using a ref-counted GC. I'm wondering if stack allocated objects get destructed when a thread gets killed.

推荐答案

当你杀死堆栈时,堆栈不会解开'a thread。

The stack does not unwind when you 'kill' a thread.

杀死线程不是一种可靠的操作方式 - 它们打开的资源(如文件)在进程关闭之前保持打开状态。此外,如果他们在关闭任何锁时保持打开,则锁可能保持锁定。记住,你可能会调用很多你不能控制的平台代码,你不能总是看到这些东西。

Killing threads is not a robust way to operate - resources they have open, such as files, remain open until the process closes. Furthermore, if they hold open any locks at the time you close them, the lock likely remains locked. Remember, you are likely calling a lot of platform code you do not control and you can't always see these things.

关闭线程的优雅的方法是中断它 - 通常它会轮询,看看是否被告知定期关闭,或者它正在运行一个消息循环,并发送一个退出消息。

The graceful robust way to close a thread is to interrupt it - typically it will poll to see if it's been told to close down periodically, or it's running a message loop and you send it a quit message.

这篇关于当我在C ++中杀了一个pThread,堆栈上的对象的析构函数是否被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 03:40