当前位置:网站首页>【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中,大佬们有其他的线程使用和管理的好方法也请指点一下啦
边栏推荐
- linux下清理系统缓存并释放内存
- 100年仅6款产品获批,疫苗竞争背后的“佐剂”江湖
- The difference between NiO and traditional IO
- Classify boost libraries by function
- Learning notes on futuretask source code of concurrent programming series
- plantuml介绍与使用
- 为什么数字化转型战略必须包括持续测试?
- "The silk road is in its youth and looks at Fujian" is in the hot collection of works in the Fujian foreign youth short video competition
- QT uses ffmpeg4 to convert the qimage of ARGB to yuv422p
- #yyds干货盘点# 解决名企真题:扭蛋机
猜你喜欢
AirServer2022最新版功能介绍及下载
GenICam GenTL 标准 ver1.5(4)第五章 采集引擎
【MySQL】explain的基本使用以及各列的作用
Little p weekly Vol.11
Relationship and difference between enterprise architecture and project management
互联网的智算架构设计
13th Blue Bridge Cup group B national tournament
黑马程序员-软件测试--06阶段2-linux和数据库-01-08第一章-linux操作系统阶段内容说明,linux命令基本格式以及常见形式的说明,操作系统的常见的分类,查看命令帮助信息方法,
The second anniversary of the three winged bird: the wings are getting richer and the take-off is just around the corner
Mask wearing detection method based on yolov5
随机推荐
Gaussdb (DWS) active prevention and troubleshooting
Unity 使用Sqlite
Airserver mobile phone third-party screen projection computer software
Communication between browser tab pages
Count the number of each character in the character
Aidl basic use
MySQL系列之事务日志Redo log学习笔记
MySQL的存储过程
Spark interview questions
What is the difference between consonants and Initials? (difference between initials and consonants)
从零开始学 MySQL —数据库和数据表操作
RestTemplate 远程调用工具类
二叉树的基本操作
Burpsuite simple packet capturing tutorial [easy to understand]
[monomer] recommended configuration of streaming information i-bpsv3 server
【目标跟踪】|单目标跟踪指标
List announced | outstanding intellectual property service team in China in 2021
【单体】流辰信息I-BPSv3服务器推荐配置
MySQL MHA high availability configuration and failover
LC501. 二叉搜索树中的众数