注意:释放线程的时候触发线程的信号与槽连接时的连接类型参数一定要是Qt::ConnectionType::DirectConnection,否则线程销毁不了会造成内存泄露,通过任务栏开启资源管理器可监视cup的线程数变化情况。
QThread* th=new QThread();
Work* mywork=new Work ();
mywork->moveToThread(th);
SigManager sm;
connect(th, &QThread::finished, mywork, &QObject::deleteLater);//线程结束释放工作类
connect(th, &QThread::finished, th, &QObject::deleteLater);//线程结束释放线程指针
QObject::connect(mywork,&Work::SigExitThread,th,&QThread::exit,Qt::ConnectionType::DirectConnection);//退出线程
QObject::connect(mywork,&Work::SigExitThread,th,&QThread::quit,Qt::ConnectionType::DirectConnection);//退出线程
QObject::connect(mywork,&Work::SigExitThread,th,&QThread::terminate,Qt::ConnectionType::DirectConnection);//退出线程
QObject::connect(&sm, &SigManager::Sig_ExcuteFunction,mywork,&Work::Function1);文章来源:https://www.toymoban.com/news/detail-785926.html
th->start();文章来源地址https://www.toymoban.com/news/detail-785926.html
到了这里,关于QT中Qthread线程彻底销毁的实例与注意事项(防止线程资源内存泄露)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!