当前位置:网站首页>【QT小作】封装一个简单的线程管理类
【QT小作】封装一个简单的线程管理类
2022-07-01 21:47:00 【林柒Sevenlin】
目的
在使用moveToThread的方式创建线程时,创建和销毁这个部分的代码总是要重复编写,所以就想着把这部分代码拎出来封装成一个类,用来负责这部分的功能,并且用来管理创建的线程。
代码
.h文件
#ifndef LQOBJTHREADPOOL_H
#define LQOBJTHREADPOOL_H
/**
* @className LQObjThreadPool
* @brief 线程管理
* @author Sevenlin
* @date 2022-06-30
* @function
* version:v1.0.0 date:2022-06-30
* 1. 线程的创建和销毁
* 2. 使用moveToThread的方式创建线程
*/
#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 获取单例对象
* @return
*/
static LQObjThreadPool *getInstance();
/**
* @brief createThread 添加线程
* @param obj QObject对象
* @param isAutoDelete QObject对象的释放是否由LQObjThreadPool对象负责
* @return 1表示添加成功、-1表示该obj已创建线程、-2表示该obj不能指定父对象
*/
int createThread(QObject *obj, bool isAutoDelete = true);
/**
* @brief removeThread 移除线程
* @param obj QObject对象
* @return 1表示删除成功、-1表示该obj尚未创建线程、-2表示该obj不能指定父对象
*/
int removeThread(QObject *obj);
private:
QMap<QObject *, QThread *> m_mapObjThread;
};
#endif // LQOBJTHREADPOOL_H
.cpp文件
#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中,大佬们有其他的线程使用和管理的好方法也请指点一下啦
边栏推荐
- Do you want to make up for the suspended examination in the first half of the year? Including ten examinations for supervision engineers, architects, etc
- MQ learning notes
- Show member variables and methods in classes in idea
- Dark horse programmer - software testing - stage 06 2-linux and database-01-08 Chapter 1 - description of the content of the Linux operating system stage, description of the basic format and common fo
- Learning notes on futuretask source code of concurrent programming series
- One of the basic learning of function
- 内存导致的电脑游戏中显示hdmi无信号 从而死机的情况
- Go - exe corresponding to related dependency
- The second anniversary of the three winged bird: the wings are getting richer and the take-off is just around the corner
- Slope compensation
猜你喜欢
Classify boost libraries by function
Basic knowledge of ngnix
Mysql——》MyISAM存储引擎的索引
Training on the device with MIT | 256Kb memory
Flume interview questions
MySQL系列之事务日志Redo log学习笔记
【MySQL】索引的分类
Learn MySQL from scratch - database and data table operations
Spark interview questions
【MySQL】explain的基本使用以及各列的作用
随机推荐
Yan Rong looks at how to formulate a multi cloud strategy in the era of hybrid cloud
What is the difference between consonants and Initials? (difference between initials and consonants)
Mysql——》索引存储模型推演
PyTorch磨刀篇|argmax和argmin函数
GaussDB(DWS)主动预防排查
Yyds dry goods inventory # solve the real problem of famous enterprises: egg twisting machine
Recent public ancestor offline practice (tarjan)
快乐数[环类问题之快慢指针]
100年仅6款产品获批,疫苗竞争背后的“佐剂”江湖
黑马程序员-软件测试--06阶段2-linux和数据库-01-08第一章-linux操作系统阶段内容说明,linux命令基本格式以及常见形式的说明,操作系统的常见的分类,查看命令帮助信息方法,
【MySQL】索引的创建、查看和删除
为什么数字化转型战略必须包括持续测试?
【图像分割】2021-SegFormer NeurIPS
[STM32] stm32cubemx tutorial II - basic use (new projects light up LED lights)
Airserver mobile phone third-party screen projection computer software
Count the number of each character in the character
【MySQL】explain的基本使用以及各列的作用
C#/VB.NET 给PDF文档添加文本/图像水印
Training on the device with MIT | 256Kb memory
[ecological partner] Kunpeng system engineer training