当前位置:网站首页>【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中,大佬们有其他的线程使用和管理的好方法也请指点一下啦
边栏推荐
猜你喜欢
![[noip2013] building block competition [noip2018] road laying greed / difference](/img/d1/a56231cd4eb3cc1d91d8a55048ccfe.png)
[noip2013] building block competition [noip2018] road laying greed / difference

Recent public ancestor (LCA) online practices

Object memory layout

Pytorch sharpening chapter | argmax and argmin functions

Mysql——》索引存储模型推演

MySQL series transaction log redo log learning notes

Fully annotated SSM framework construction

Go - exe corresponding to related dependency

GenICam GenTL 标准 ver1.5(4)第五章 采集引擎

"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
随机推荐
Recent public ancestor (LCA) online practices
Make a three digit number of all daffodils "recommended collection"
Airserver mobile phone third-party screen projection computer software
Why must digital transformation strategies include continuous testing?
plantuml介绍与使用
完全注解的ssm框架搭建
园区全光技术选型-中篇
详解JMM
Four methods of JS array splicing [easy to understand]
内存导致的电脑游戏中显示hdmi无信号 从而死机的情况
Can you get a raise? Analysis on gold content of PMP certificate
Show member variables and methods in classes in idea
Which securities company should we choose to open an account for flush stock? Is it safe to open an account with a mobile phone?
[noip2013] building block competition [noip2018] road laying greed / difference
对象内存布局
基准环路增益与相位裕度的测量
Qtreeview+qabstractitemmodel custom model: the third of a series of tutorials [easy to understand]
20220701
[STM32] stm32cubemx tutorial II - basic use (new projects light up LED lights)
Communication between browser tab pages