我有一个具有许多线程的应用程序。我想在关闭主应用程序时退出线程并调用线程析构函数进行必要的清理。
Class Thread :public QThread{
Thread();
run(){
while(1){
//do work
}
}
~Thread(){
//want to make clean up
}
};
Class my_app :public QCoreapplication{
my_app(){
Thread th1;
connect(&th1,SIGNAL(finished()),&th1,deleteLater());
connect(&th1,SIGNAL(finished()),&th1,quit());
}
};
//And my th1 thread runs in while.So I know that is the problem it runs on while and never emits the finished signal
//How can be achievable?
最佳答案
- Do not subclass QThread
- 在工作循环中提供一些线程安全标志,指示工作人员应该结束其工作。
- 在终止应用程序之前,您应该调用 QThread::wait确保线程在应用程序终止之前正常结束。
关于c++ - 在 Qt 中使用完成信号退出线程并进行 clean_up 的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23268331/