当前位置:网站首页>【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中,大佬们有其他的线程使用和管理的好方法也请指点一下啦
边栏推荐
- Easyexcel complex data export
- 黑马程序员-软件测试--06阶段2-linux和数据库-01-08第一章-linux操作系统阶段内容说明,linux命令基本格式以及常见形式的说明,操作系统的常见的分类,查看命令帮助信息方法,
- Mask wearing detection method based on yolov5
- What is the difference between consonants and Initials? (difference between initials and consonants)
- keras训练的H5模型转tflite
- BlocProvider 为什么感觉和 Provider 很相似?
- Internet of things RFID, etc
- List announced | outstanding intellectual property service team in China in 2021
- GenICam GenTL 标准 ver1.5(4)第五章 采集引擎
- 信标委云原生专题组组长,任重道远!
猜你喜欢

Classify boost libraries by function

详解Kubernetes网络模型

互联网的智算架构设计

焱融看 | 混合云时代下,如何制定多云策略

QT版本华睿相机的Demo程序实现

Flume interview questions

Mysql——》Innodb存储引擎的索引

Aidl basic use

Mask wearing detection method based on yolov5

黑马程序员-软件测试--06阶段2-linux和数据库-01-08第一章-linux操作系统阶段内容说明,linux命令基本格式以及常见形式的说明,操作系统的常见的分类,查看命令帮助信息方法,
随机推荐
Is PMP certificate really useful?
MySQL learning notes - SQL optimization of optimization
CSDN购买的课程从哪里可以进入
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
首席信息官对高绩效IT团队定义的探讨和分析
多种智能指针
园区全光技术选型-中篇
Chapter 9 Yunji datacanvas company has been ranked top 3 in China's machine learning platform market
Indicator trap: seven KPI mistakes that it leaders are prone to make
Learning notes on futuretask source code of concurrent programming series
Yyds dry goods inventory # solve the real problem of famous enterprises: egg twisting machine
Redis配置与优化
I received a letter from CTO inviting me to interview machine learning engineer
Basic operation of binary tree
Clean up system cache and free memory under Linux
BlocProvider 为什么感觉和 Provider 很相似?
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
Learn MySQL from scratch - database and data table operations
灵动微 MM32 多路ADC-DMA配置
Burpsuite simple packet capturing tutorial [easy to understand]