Qt文档阅读笔记-QThreadPool官方解析及实例

傷城~ 2022-11-07 04:19 286阅读 0赞

QThreadPool管理及回收QThread对象,用来减少线程创建时带来的系统开销。每一个Qt进程都有个全局的QThreadPool对象,可以通过globalInstance()获取其对象。

要想使用QThreadPool中的线程,需要先创建一个类,这个类继承QRunnable,并且要重写其run()方法。随后创建其对象,调用QThreadPool::start()方法,如下实例代码:

  1. class HelloWorldTask : public QRunnable
  2. {
  3. void run() override
  4. {
  5. qDebug() << "Hello world from thread" << QThread::currentThread();
  6. }
  7. };
  8. HelloWorldTask *hello = new HelloWorldTask();
  9. // QThreadPool takes ownership and deletes 'hello' automatically
  10. QThreadPool::globalInstance()->start(hello);

QThreadPool会自动释放QRunnable的子类,使用QRunnable::setAutoDelete()改变auto-deletion标识。

QThreadPool支持多次执行相同的QRunnable线程。这里推荐使用tryStart(this)。当最后一个线程执行结束,QRunnable就会被自动释放掉。

启用autoDelete后,调用start()多次启动相同的进程时,会形成竞争条件,不建议这么使用。

在一定时间内未被使用的线程会过期,默认是30s,可以通过setExpiryTimeout()来设置。

setMaxThreadCount()设置最大线程数maxThreadCount()用于查询.

maxThreadCount()的默认值为QThread::idealThreadCount(),activeThreadCount()返回当前正在工作的线程数。

reserveThread()函数是在线程外使用的。当调用releaseThread()来释放线程时,这个线程可能会被重复利用,这时reserveThread()就可以得到releaThread()的那个线程。

下面是自己写的一个小栗子

功能是这样的:

写一个线程,这个线程放到QThreadPool,里面,并且线程池每秒调用一次,QThreadPool里面的线程,把当前时间发送给主线程。然后打印。

还是有点意思的!

程序运行截图如下:

20210311092236643.png

源码如下:

Com.h

  1. #ifndef COM_H
  2. #define COM_H
  3. #include <QObject>
  4. QT_BEGIN_NAMESPACE
  5. class QThreadPool;
  6. class QTimer;
  7. QT_END_NAMESPACE
  8. class Com : public QObject
  9. {
  10. Q_OBJECT
  11. public:
  12. Com(QObject *parent = nullptr);
  13. protected slots:
  14. void timeOut();
  15. void getTime(const QString &time);
  16. private:
  17. QThreadPool *m_pool;
  18. QTimer *m_timer;
  19. };
  20. #endif // COM_H

Woork.h

  1. #ifndef WORK_H
  2. #define WORK_H
  3. #include <QObject>
  4. #include <QRunnable>
  5. class Work : public QObject, public QRunnable
  6. {
  7. Q_OBJECT
  8. public:
  9. Work();
  10. ~Work();
  11. protected:
  12. void run() Q_DECL_OVERRIDE;
  13. signals:
  14. void nowTime(const QString &time);
  15. };
  16. #endif // WORK_H

Com.cpp

  1. #include "Com.h"
  2. #include "Woork.h"
  3. #include <QTimer>
  4. #include <QThreadPool>
  5. #include <QDebug>
  6. Com::Com(QObject *parent) : QObject(parent)
  7. {
  8. m_timer = new QTimer(this);
  9. m_pool = new QThreadPool(this);
  10. m_pool->setMaxThreadCount(1);
  11. connect(m_timer, &QTimer::timeout, this, &Com::timeOut);
  12. m_timer->start(1000);
  13. }
  14. void Com::timeOut()
  15. {
  16. Work *work = new Work;
  17. connect(work, &Work::nowTime, this, &Com::getTime);
  18. m_pool->start(work);
  19. }
  20. void Com::getTime(const QString &time)
  21. {
  22. qDebug() << "接收到:" + time;
  23. }

main.cpp

  1. #include <QCoreApplication>
  2. #include "Com.h"
  3. int main(int argc, char *argv[])
  4. {
  5. QCoreApplication a(argc, argv);
  6. Com com;
  7. return a.exec();
  8. }

Woork.cpp

  1. #include "Woork.h"
  2. #include <QDateTime>
  3. #include <QDebug>
  4. Work::Work()
  5. {
  6. qDebug() << "Work::Work()";
  7. }
  8. Work::~Work()
  9. {
  10. qDebug() << "Work::~Work()";
  11. }
  12. void Work::run()
  13. {
  14. emit this->nowTime(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"));
  15. }

源码打包下载地址:

https://github.com/fengfanchen/Qt/tree/master/QThreadPoolDemo

发表评论

表情:
评论列表 (有 0 条评论,286人围观)

还没有评论,来说两句吧...

相关阅读

    相关 QT QThreadPool

    QThreadPool类管理一个QThreads集合。 QThreadPool管理和回收单独的QThread对象,以帮助减少使用线程的程序中创建线程的成本。每个Qt应用程序