Qt多线程-QThreadPool线程池与QRunnable
本文標題:Qt多線程-QThreadPool線程池與QRunnable?????本文地址:http://techieliang.com/2017/12/605/ 文章目錄
- 1. 介紹
- 2. QThreadPool
- ?2.1. 基本操作函數
- ?2.2. start tryStart tryTake
- ?2.3. 全局線程池
- ?2.4. 局部線程池
- 3. QRunnable
- 4. 范例
- ?4.1. 簡單使用范例
- ?4.2. 全局線程池和局部線程池對比
1. 介紹
線程的創建及銷毀需要與系統交互,會產生很大的開銷。若需要頻繁的創建線程建議使用線程池,有線程池維護一定數量的線程,當需要進行多線程運算時將運算函數傳遞給線程池即可。線程池會根據可用線程進行任務安排。
2. QThreadPool
相關幫助文檔:QThreadPool
此類為Qt提供的線程池函數,使用此類只需要配置線程池的最大線程數量、線程長時間不使用的過期時間等參數,不需要進行QThread相關的操作。
此類有兩種使用方式:全局線程池和局部線程池。下面首先介紹兩種類型后續介紹類提供的方法
2.1. 基本操作函數
QThread::idealThreadCount函數,會根據當前設備的硬件情況給出一個線程數量,而maxThreadCount的默認值就是此值。
setStackSize
只有在線程池創建新線程時才使用該屬性的值。更改它對已經創建或運行的線程沒有影響。默認值是0,這使得qthread使用操作系統默認的堆棧大小。
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 后的線程不計入線程數量,因此可能出現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
對于start,傳入的是QRunnable對象指針,傳入后線程池會調用QRunnable的autoDelete() 函數,若返回true,則當此運算完成后自動釋放內容,不需要后續主動判斷是否運算完成并釋放空間。
對于tryTake,若返回成功,不會自動釋放內容,而是需要調用方主動釋放,無論autodelete返回值是什么。返回false自然也不會自動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.
對于tryStart,若返回成功,等同于start,若false,則不會自動delete
注意,對于autoDelete必須在調用state/trytake之前進行修改,不要再調用以后修改,否則結果不可預測
Note that changing the auto-deletion on?runnable?after calling this function results in undefined behavior.
QRunnable的autoDelete默認返回true,若需要更改需要調用setAutoDelete進行更改
2.3. 全局線程池
QThreadPool提供了一個靜態函數,globalInstance(),使用此方法可獲取一個當前進程的全局線程池,可在多個類中共同使用一個線程池。
2.4. 局部線程池
和常規類的使用相同,可以通過? QThreadPool pool;的方式建立一個局部線程池,并由當前類維護,可保證此線程池僅供當前類應用
3. QRunnable
線程池每一個需要運行的任務均需要作為QRunnable的子類,并重寫其run函數,幫助文檔:http://doc.qt.io/qt-5/qrunnable.html
QRunnable只有run、autodelete、setautodelete這三個關鍵函數。
run內重寫需要運算的內容。
autodelete用來標識是否在運行結束后自動由線程池釋放空間,具體說明見上述“QThreadPool-基本操作函數-start tryStart tryTake”
4. 范例
4.1. 簡單使用范例
結果:
4.2. 全局線程池和局部線程池對比
結果
當建立局部線程池,修改其參數后僅供局部使用,不會影響全局線程池的。
轉載請以鏈接形式標明本文標題和地址:Techie亮博客???Qt多線程-QThreadPool線程池與QRunnable總結
以上是生活随笔為你收集整理的Qt多线程-QThreadPool线程池与QRunnable的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt文档阅读笔记-QGraphicsIt
- 下一篇: C++多继承与虚继承