当前位置:网站首页>[QT widget] encapsulates a simple thread management class
[QT widget] encapsulates a simple thread management class
2022-07-01 22:49:00 【Lin Qi sevenlin】
Purpose
In the use of moveToThread When creating a thread , The code that creates and destroys this part is always written repeatedly , So I want to package this part of code into a class , Used to take charge of the functions of this part , And used to manage the threads created .
Code
.h file
#ifndef LQOBJTHREADPOOL_H
#define LQOBJTHREADPOOL_H
/**
* @className LQObjThreadPool
* @brief Thread management
* @author Sevenlin
* @date 2022-06-30
* @function
* version:v1.0.0 date:2022-06-30
* 1. Thread creation and destruction
* 2. Use moveToThread To create a thread
*/
#include <QObject>
#include <QMap>
class QThread;
class LQObjThreadPool : public QObject
{
Q_OBJECT
public:
explicit LQObjThreadPool(QObject *parent = nullptr);
~LQObjThreadPool();
Q_DISABLE_COPY(LQObjThreadPool);
public:
/**
* @brief getInstance Get singleton object
* @return
*/
static LQObjThreadPool *getInstance();
/**
* @brief createThread Add thread
* @param obj QObject object
* @param isAutoDelete QObject Whether the object is released by LQObjThreadPool The object is responsible for
* @return 1 Indicates successful addition 、-1 It means that we should obj Thread created 、-2 It means that we should obj Cannot specify parent object
*/
int createThread(QObject *obj, bool isAutoDelete = true);
/**
* @brief removeThread Remove thread
* @param obj QObject object
* @return 1 Indicates that the deletion was successful 、-1 It means that we should obj Thread has not been created 、-2 It means that we should obj Cannot specify parent object
*/
int removeThread(QObject *obj);
private:
QMap<QObject *, QThread *> m_mapObjThread;
};
#endif // LQOBJTHREADPOOL_H
.cpp file
#include "lqobjthreadpool.h"
#include <QThread>
LQObjThreadPool::LQObjThreadPool(QObject *parent) : QObject(parent)
{
}
LQObjThreadPool::~LQObjThreadPool()
{
int count = m_mapObjThread.count();
if (count > 0) {
QList<QObject *> objs = m_mapObjThread.keys();
for (auto &obj : objs) {
removeThread(obj);
}
}
}
LQObjThreadPool *LQObjThreadPool::getInstance()
{
static LQObjThreadPool *threadPool = new LQObjThreadPool();
return threadPool;
}
int LQObjThreadPool::createThread(QObject *obj, bool isAutoDelete)
{
if (obj->parent()) {
return -2;
}
if (m_mapObjThread.contains(obj)) {
return -1;
}
QThread *thread = new QThread(this);
obj->moveToThread(thread);
if (isAutoDelete) {
connect(thread, &QThread::finished, obj, &QObject::deleteLater);
connect(obj, &QObject::destroyed, [&obj] {
obj = nullptr;
});
}
thread->start();
m_mapObjThread.insert(obj, thread);
return 1;
}
int LQObjThreadPool::removeThread(QObject *obj)
{
if (obj->parent()) {
return -2;
}
if (!m_mapObjThread.contains(obj)) {
return -1;
}
QThread *thread = m_mapObjThread.take(obj);
thread->quit();
thread->wait();
return 1;
}
QT in , Big guys have other good ways to use and manage threads. Please also point out
边栏推荐
猜你喜欢
互联网的智算架构设计
好友新书发布,祝贺(送福利)
Appium自动化测试基础 — 补充:Desired Capabilities参数介绍
Fully annotated SSM framework construction
Fiori applications are shared through the enhancement of adaptation project
2020-ViT ICLR
功能测试报告的编写
Delete AWS bound credit card account
Recent public ancestor offline practice (tarjan)
Single step debugging analysis of rxjs observable of operator
随机推荐
每日刷题记录 (十)
QT uses ffmpeg4 to convert the qimage of ARGB to yuv422p
[untitled]
Pytorch sharpening chapter | argmax and argmin functions
[C language] detailed explanation of malloc function [easy to understand]
mixconv代码
Configure filter
Mysql——》Innodb存储引擎的索引
H5 model trained by keras to tflite
Easyexcel complex data export
Fiori applications are shared through the enhancement of adaptation project
el-input文本域字数限制,超过显示变红并禁止输入
下班前几分钟,我弄清了v-model与.sync的区别
Ffmpeg learning notes
Recent public ancestor offline practice (tarjan)
RestTemplate 远程调用工具类
Slope compensation
SAP 智能机器人流程自动化(iRPA)解决方案分享
Niuke monthly race - logarithmic sum in groups
死锁的处理策略—预防死锁、避免死锁、检测和解除死锁