Qt多线程-QThreadPool线程池与QRunnable
本文標(biāo)題:Qt多線程-QThreadPool線程池與QRunnable?????本文地址:http://techieliang.com/2017/12/605/ 文章目錄
- 1. 介紹
- 2. QThreadPool
- ?2.1. 基本操作函數(shù)
- ?2.2. start tryStart tryTake
- ?2.3. 全局線程池
- ?2.4. 局部線程池
- 3. QRunnable
- 4. 范例
- ?4.1. 簡(jiǎn)單使用范例
- ?4.2. 全局線程池和局部線程池對(duì)比
1. 介紹
線程的創(chuàng)建及銷毀需要與系統(tǒng)交互,會(huì)產(chǎn)生很大的開銷。若需要頻繁的創(chuàng)建線程建議使用線程池,有線程池維護(hù)一定數(shù)量的線程,當(dāng)需要進(jìn)行多線程運(yùn)算時(shí)將運(yùn)算函數(shù)傳遞給線程池即可。線程池會(huì)根據(jù)可用線程進(jìn)行任務(wù)安排。
2. QThreadPool
相關(guān)幫助文檔:QThreadPool
此類為Qt提供的線程池函數(shù),使用此類只需要配置線程池的最大線程數(shù)量、線程長時(shí)間不使用的過期時(shí)間等參數(shù),不需要進(jìn)行QThread相關(guān)的操作。
此類有兩種使用方式:全局線程池和局部線程池。下面首先介紹兩種類型后續(xù)介紹類提供的方法
2.1. 基本操作函數(shù)
QThread::idealThreadCount函數(shù),會(huì)根據(jù)當(dāng)前設(shè)備的硬件情況給出一個(gè)線程數(shù)量,而maxThreadCount的默認(rèn)值就是此值。
setStackSize
只有在線程池創(chuàng)建新線程時(shí)才使用該屬性的值。更改它對(duì)已經(jīng)創(chuàng)建或運(yùn)行的線程沒有影響。默認(rèn)值是0,這使得qthread使用操作系統(tǒng)默認(rèn)的堆棧大小。
The value of the property is only used when the thread pool creates new threads. Changing it has no effect for already created or running threads.
The default value is 0, which makes?QThread?use the operating system default stack size.
maxThreadCount? reserveThread? activeThreadCount
由于reserveThread 后的線程不計(jì)入線程數(shù)量,因此可能出現(xiàn)activeThreadCount>maxThreadCount? 情況
Note:?It is possible for this function to return a value that is greater than?maxThreadCount(). See?reserveThread() for more details.
2.2. start tryStart tryTake
對(duì)于start,傳入的是QRunnable對(duì)象指針,傳入后線程池會(huì)調(diào)用QRunnable的autoDelete() 函數(shù),若返回true,則當(dāng)此運(yùn)算完成后自動(dòng)釋放內(nèi)容,不需要后續(xù)主動(dòng)判斷是否運(yùn)算完成并釋放空間。
對(duì)于tryTake,若返回成功,不會(huì)自動(dòng)釋放內(nèi)容,而是需要調(diào)用方主動(dòng)釋放,無論autodelete返回值是什么。返回false自然也不會(huì)自動(dòng)delete
Attempts to remove the specified?runnable?from the queue if it is not yet started. If the runnable had not been started, returns?true, and ownership of?runnable?is transferred to the caller (even when?runnable->autoDelete() == true). Otherwise returns?false.
Note:?If?runnable->autoDelete() == true, this function may remove the wrong runnable. This is known as the?ABA problem: the original?runnable?may already have executed and has since been deleted. The memory is re-used for another runnable, which then gets removed instead of the intended one. For this reason, we recommend calling this function only for runnables that are not auto-deleting.
對(duì)于tryStart,若返回成功,等同于start,若false,則不會(huì)自動(dòng)delete
注意,對(duì)于autoDelete必須在調(diào)用state/trytake之前進(jìn)行修改,不要再調(diào)用以后修改,否則結(jié)果不可預(yù)測(cè)
Note that changing the auto-deletion on?runnable?after calling this function results in undefined behavior.
QRunnable的autoDelete默認(rèn)返回true,若需要更改需要調(diào)用setAutoDelete進(jìn)行更改
2.3. 全局線程池
QThreadPool提供了一個(gè)靜態(tài)函數(shù),globalInstance(),使用此方法可獲取一個(gè)當(dāng)前進(jìn)程的全局線程池,可在多個(gè)類中共同使用一個(gè)線程池。
2.4. 局部線程池
和常規(guī)類的使用相同,可以通過? QThreadPool pool;的方式建立一個(gè)局部線程池,并由當(dāng)前類維護(hù),可保證此線程池僅供當(dāng)前類應(yīng)用
3. QRunnable
線程池每一個(gè)需要運(yùn)行的任務(wù)均需要作為QRunnable的子類,并重寫其run函數(shù),幫助文檔:http://doc.qt.io/qt-5/qrunnable.html
QRunnable只有run、autodelete、setautodelete這三個(gè)關(guān)鍵函數(shù)。
run內(nèi)重寫需要運(yùn)算的內(nèi)容。
autodelete用來標(biāo)識(shí)是否在運(yùn)行結(jié)束后自動(dòng)由線程池釋放空間,具體說明見上述“QThreadPool-基本操作函數(shù)-start tryStart tryTake”
4. 范例
4.1. 簡(jiǎn)單使用范例
結(jié)果:
4.2. 全局線程池和局部線程池對(duì)比
結(jié)果
當(dāng)建立局部線程池,修改其參數(shù)后僅供局部使用,不會(huì)影響全局線程池的。
轉(zhuǎn)載請(qǐng)以鏈接形式標(biāo)明本文標(biāo)題和地址:Techie亮博客???Qt多線程-QThreadPool線程池與QRunnable總結(jié)
以上是生活随笔為你收集整理的Qt多线程-QThreadPool线程池与QRunnable的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt文档阅读笔记-QGraphicsIt
- 下一篇: C++多继承与虚继承