当前位置:网站首页>【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中,大佬们有其他的线程使用和管理的好方法也请指点一下啦
边栏推荐
- CSDN购买的课程从哪里可以进入
- RestTemplate 远程调用工具类
- Fully annotated SSM framework construction
- Why must digital transformation strategies include continuous testing?
- 91.(cesium篇)cesium火箭发射模拟
- Which securities company should we choose to open an account for flush stock? Is it safe to open an account with a mobile phone?
- 比较版本号[双指针截取自己想要的字串]
- [live broadcast review] the first 8 live broadcasts of battle code Pioneer have come to a perfect end. Please look forward to the next one!
- The correct way to set the bypass route
- Make a three digit number of all daffodils "recommended collection"
猜你喜欢

H5 model trained by keras to tflite

In the past 100 years, only 6 products have been approved, which is the "adjuvant" behind the vaccine competition

功能测试报告的编写

I received a letter from CTO inviting me to interview machine learning engineer

配置筛选机

固定资产管理子系统报表分为什么大类,包括哪些科目

Learning notes on futuretask source code of concurrent programming series

【JetCache】JetCache的使用方法与步骤

Basic operation of binary tree

详解Kubernetes网络模型
随机推荐
Redis配置与优化
Using closures to switch toggle by clicking a button
Unity 使用Sqlite
Why must digital transformation strategies include continuous testing?
焱融看 | 混合云时代下,如何制定多云策略
Introduction and download of the latest version of airserver2022
leetcode - 287. 寻找重复数
[intelligent QBD risk assessment tool] Shanghai daoning brings you leanqbd introduction, trial and tutorial
[commercial terminal simulation solution] Shanghai daoning brings you Georgia introduction, trial and tutorial
Mask wearing detection method based on yolov5
Can you get a raise? Analysis on gold content of PMP certificate
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
CSDN购买的课程从哪里可以进入
Is PMP certificate really useful?
Interview question: what is the difference between MySQL's Union all and union, and how many join methods MySQL has (Alibaba interview question) [easy to understand]
PyTorch磨刀篇|argmax和argmin函数
互联网的智算架构设计
Aidl basic use
100年仅6款产品获批,疫苗竞争背后的“佐剂”江湖
【MySQL】数据库优化方法