Qt文档阅读笔记-QThreadPool的解释及使用
目錄
Detailed Description
博主栗子
關(guān)于全局線程池使用以及是否自動(dòng)銷毀
關(guān)于最大線程的運(yùn)行栗子
Detailed Description
QThreadPool類用于管理QThreads的集合。
QThreadPool管理以及重復(fù)利用每一個(gè)線程對(duì)象,來(lái)幫助減少創(chuàng)建線程時(shí)帶來(lái)的系統(tǒng)開銷。每一個(gè)Qt應(yīng)用程序都有一個(gè)全局的QThreadPool對(duì)象,可以直接通過調(diào)用globalInstance()進(jìn)行訪問。
要想使用QThreadPool里面的線程,要先繼承QRunnable并且重寫run()函數(shù),然后創(chuàng)建一個(gè)對(duì)象并且通過使用QThreadPool::start()進(jìn)行調(diào)用
class HelloWorldTask : public QRunnable{void run(){qDebug() << "Hello world from thread" << QThread::currentThread();}}HelloWorldTask *hello = new HelloWorldTask();// QThreadPool takes ownership and deletes 'hello' automaticallyQThreadPool::globalInstance()->start(hello);默認(rèn)情況下,QThreadPool自動(dòng)刪除QRunnable。但可以通過使用QRunable::setAutoDelete()去改變自動(dòng)刪除這個(gè)功能。
QThreadPool支持執(zhí)行相同的QRunnable類,這種騷操作可以在QRunnable::run()中調(diào)用tryStart(this)。當(dāng)自動(dòng)刪除被激活的時(shí)候,QRunnable將在最后一個(gè)線程運(yùn)行函數(shù)調(diào)用并且結(jié)束后,被調(diào)用(自動(dòng)刪除)。當(dāng)autoDelete被激活的時(shí)候用一個(gè)相同的QRunnable調(diào)用多次start(),會(huì)創(chuàng)造線程的競(jìng)爭(zhēng)條件,官方不推薦這么做。
在一定時(shí)間內(nèi)沒有使用的線程將會(huì)死亡,這個(gè)默認(rèn)死亡的時(shí)間是30s。但可以通過使用setExpiryTimeout()函數(shù)來(lái)改變這個(gè)到期的值。通過設(shè)置一個(gè)與時(shí)間之前的值(比如現(xiàn)在是9點(diǎn)半,設(shè)置成9點(diǎn))那么這個(gè)到期的機(jī)制將會(huì)被終止。
調(diào)用maxThreadCount()能查詢到可以使用的最大線程的個(gè)數(shù),想改變他的話可以通過調(diào)用setMaxThreadCount()進(jìn)行設(shè)置。默認(rèn)的maxThreadCount()的數(shù)量是QThread::idealThreadCount()。activeThreaCount()返回目前正在工作的線程的數(shù)量。
reserveThread()函數(shù)保留了一個(gè)線程用于線程池外部使用。通過調(diào)用releaseThread()進(jìn)行使用,這個(gè)線程是不會(huì)到期的,可以重復(fù)使用。可以通過使用這些函數(shù)去臨時(shí)的增加或減少活動(dòng)的線程的數(shù)量,并且還不對(duì)系統(tǒng)的時(shí)間開銷還不高。
【注意:QThreadPool是低級(jí)的線程管理類,Qt Concurrent module是高級(jí)管理類,他是QThreadPool的代替選擇】
?
博主栗子
關(guān)于全局線程池使用以及是否自動(dòng)銷毀
程序運(yùn)行截圖如下:
源碼如下:
worker.h
#ifndef WORKER_H #define WORKER_H#include <QRunnable>class Worker : public QRunnable { public:explicit Worker();~Worker();void run(); };#endif // WORKER_Hworker.cpp
#include "worker.h" #include <QDebug> #include <QThread>Worker::Worker() {}Worker::~Worker() {qDebug()<<"go home, dog bye"; }void Worker::run() {qDebug()<<"Hello world from thread "<<QThread::currentThreadId(); }mainc.pp
#include "worker.h" #include <QApplication> #include <QThreadPool>int main(int argc, char *argv[]) {QApplication a(argc, argv);Worker *worker=new Worker;QThreadPool::globalInstance()->start(worker);return a.exec(); }可以看到他自動(dòng)調(diào)用了析構(gòu)函數(shù),他在內(nèi)部對(duì)他進(jìn)行delete(對(duì)于C++程序員來(lái)說,這有點(diǎn)爽啊)
?
關(guān)于最大線程的運(yùn)行栗子
運(yùn)行截圖如下:
代碼如下:
worker.h
#ifndef WORKER_H #define WORKER_H#include <QRunnable>class Worker : public QRunnable { public:explicit Worker();~Worker();void run(); };#endif // WORKER_Hworker.cpp
#include "worker.h" #include <QDebug> #include <QThread>Worker::Worker() {//this->setAutoDelete(false); }Worker::~Worker() {qDebug()<<"go home, dog bye"; }void Worker::run() {QThread::msleep(1000);qDebug()<<"Hello world from thread "<<QThread::currentThreadId(); }main.cpp
#include "worker.h" #include <QApplication> #include <QThreadPool>int main(int argc, char *argv[]) {QApplication a(argc, argv);Worker *worker[100];for(int i=0;i<100;i++){worker[i]=new Worker;}QThreadPool::globalInstance()->setMaxThreadCount(2);for(int i=0;i<100;i++){QThreadPool::globalInstance()->start(worker[i]);}return a.exec(); }?
總結(jié)
以上是生活随笔為你收集整理的Qt文档阅读笔记-QThreadPool的解释及使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt文档阅读笔记-Qt工作笔记-QThr
- 下一篇: C++工作笔记-VS中“调用堆栈”窗口的