当前位置:网站首页>Several ways of QT thread exit
Several ways of QT thread exit
2022-07-23 08:13:00 【Love the west wind】
QTread sign out , There are several ways , Using variables ; You can also use QWaitCondition;
1. Using variables , Set the external variable to false
#define WORKOBJECT_H
#include <QObject>
#include <QThread>
#include <QDebug>
#include<QMutexLocker>
#include<QMutex>
#include<QWaitCondition>
class WorkObject : public QObject
{
Q_OBJECT
public:
explicit WorkObject(QObject *parent = nullptr);
signals:
public slots:
void doWork();
void stopWork();
private:
bool exitflag;
QWaitCondition mWaitStatusCondition;
QMutex mWaitMutex;
};
#endif // WORKOBJECT_Hcpp
#include "workobject.h"
WorkObject::WorkObject(QObject *parent) : QObject(parent)
{
}
void WorkObject::doWork()
{
while(true)
{
QMutexLocker locker(&mWaitMutex);
if(!exitflag)
{
qDebug() << "MyThread eixt ================ :" ;
return;
}
// if(mWaitStatusCondition.wait(&mWaitMutex,1))
// {
// qDebug() << "MyThread eixt ================ :" ;
// return;
// }
qDebug() << "MyThread thread id:" << QThread::currentThreadId();
//sleep(10);
//QThread::sleep(1);
}
qDebug() << "doWork thread exit========:" << QThread::currentThreadId();
}
void WorkObject::stopWork()
{
QMutexLocker locker(&mWaitMutex);
{
exitflag = false;
// mWaitStatusCondition.wakeAll();
}
}
2. You can also use QWaitCondition
QWaitCondition The biggest benefit , I thought , It can wake up one or more other threads in one thread , Of course, the premise is , Other threads are waiting for a QWaitCondition, Otherwise it won't work , It's no use waking you up .QWaitCondition Must be with QMutex perhaps QReadwriteLock Use... Together
mWaitStatusCondition.wakeAll();
#include "workobject.h"
WorkObject::WorkObject(QObject *parent) : QObject(parent)
{
}
void WorkObject::doWork()
{
while(true)
{
QMutexLocker locker(&mWaitMutex);
if(!exitflag)
{
return;
}
if(mWaitStatusCondition.wait(&mWaitMutex,1))
{
qDebug() << "MyThread eixt ================ :" ;
return;
}
qDebug() << "MyThread thread id:" << QThread::currentThreadId();
//sleep(10);
//QThread::sleep(1);
}
qDebug() << "doWork thread exit========:" << QThread::currentThreadId();
}
void WorkObject::stopWork()
{
QMutexLocker locker(&mWaitMutex);
{
//exitflag = false;
mWaitStatusCondition.wakeAll();
}
}
边栏推荐
- Worthington羟基类固醇脱氢酶技术说明及测定方案
- Binary tree (learning daily)
- matlab 分数阶pid控制
- Web resource sharing
- Rust -- detailed explanation of option
- Redis 事务学习有感
- Live broadcast preview | live broadcast Seminar on open source security governance models and tools
- 开发者分享|MindSpore Lite 体验,一键实现图像分割
- C语言函数(1)
- Google Earth Engine APP——一个完整的地图图例APP(美国西部土地利用分类)
猜你喜欢

Matlab保存数据到csv文件的方法分享

Internet traffic scheduling scheme

论文阅读:The Perfect Match: 3D Point Cloud Matching with Smoothed Densities

MySQL消息队列表结构

matlab ode45求解微分方程

数据库基础及安装

Web resource sharing

Spark troubleshooting -precondition eof: no length prefix available

【读书笔记->统计学】12-01 置信区间的构建-置信区间概念简介

将childNodes返回的伪数组转化为真数组
随机推荐
Typescript对象扩展之对象原型__proto__与prototype
Expérience II Yuv
Yolov5 post-processing code of cpu/gpu (CUDA) version
MPLS VPN 跨域-optionB
c语言扫雷
LeetCode 第26天
1.10 API and string
如何用C语言实现简单职工信息管理系统
C语言中的字符串
RequestContextHolder
matlab ode45求解微分方程
大咖訪談 | 開源社區裏各種奇怪的現狀——夜天之書陳梓立tison
Binary tree (learning daily)
Istio架构扩展机制
Etcdv3 · watch operation implementation and relevant key instructions
直播预告 | 开源安全治理模型和工具直播研讨会
Wechat applet project practice
LC:剑指 Offer 39. 数组中出现次数超过一半的数字
为什么有的人把代码写的如此复杂?
matlab声音信号处理 频率图 信号过滤和播放声音