1. 介紹
Qt除了提供基本的QThread實現多線程,並提供QThreadPool實現線程池之外,還提供了QtConcurrent模塊用於並行計算。html
使用此類須要在pro文件增長QT += concurrent多線程
QtConcurrent命名空間幫助文檔,API介紹幫助app
1.1. API
- void blockingFilter(Sequence &sequence, FilterFunction filterFunction)
- Sequence blockingFiltered(const Sequence &sequence, FilterFunction filterFunction)
- Sequence blockingFiltered(ConstIterator begin, ConstIterator end, FilterFunction filterFunction)
- T blockingFilteredReduced(const Sequence &sequence, FilterFunction filterFunction, ReduceFunction reduceFunction, QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce)
- T blockingFilteredReduced(ConstIterator begin, ConstIterator end, FilterFunction filterFunction, ReduceFunction reduceFunction, QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce)
- void blockingMap(Sequence &sequence, MapFunction function)
- void blockingMap(Iterator begin, Iterator end, MapFunction function)
- T blockingMapped(const Sequence &sequence, MapFunction function)
- T blockingMapped(ConstIterator begin, ConstIterator end, MapFunction function)
- T blockingMappedReduced(const Sequence &sequence, MapFunction mapFunction, ReduceFunction reduceFunction, QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce)
- T blockingMappedReduced(ConstIterator begin, ConstIterator end, MapFunction mapFunction, ReduceFunction reduceFunction, QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce)
- QFuture<void> filter(Sequence &sequence, FilterFunction filterFunction)
- QFuture<T> filtered(const Sequence &sequence, FilterFunction filterFunction)
- QFuture<T> filtered(ConstIterator begin, ConstIterator end, FilterFunction filterFunction)
- QFuture<T> filteredReduced(const Sequence &sequence, FilterFunction filterFunction, ReduceFunction reduceFunction, QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce)
- QFuture<T> filteredReduced(ConstIterator begin, ConstIterator end, FilterFunction filterFunction, ReduceFunction reduceFunction, QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce)
- QFuture<void> map(Sequence &sequence, MapFunction function)
- QFuture<void> map(Iterator begin, Iterator end, MapFunction function)
- QFuture<T> mapped(const Sequence &sequence, MapFunction function)
- QFuture<T> mapped(ConstIterator begin, ConstIterator end, MapFunction function)
- QFuture<T> mappedReduced(const Sequence &sequence, MapFunction mapFunction, ReduceFunction reduceFunction, QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce)
- QFuture<T> mappedReduced(ConstIterator begin, ConstIterator end, MapFunction mapFunction, ReduceFunction reduceFunction, QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce)
- QFuture<T> run(Function function, ...)
- QFuture<T> run(QThreadPool *pool, Function function, ...)
不少都是重載的,主要函數以下:async
- Concurrent Map and Map-Reduce
- QtConcurrent::map() applies a function to every item in a container, modifying the items in-place.
- –這個函數會將做爲參數傳入的函數應用到容器中的每一項,對這些項進行直接修改,會修改傳入變量內容。
- QtConcurrent::mapped() is like map(), except that it returns a new container with the modifications.
- –功能相似於map(),只不過它不是直接修改原始容器,而是將修改後的元素放到一個新的容器中並做爲返回值返回。
- QtConcurrent::mappedReduced() is like mapped(), except that the modified results are reduced or folded into a single result.
- –功能相似於mapped(),首先執行mapped的操做,而後傳入一個Reduce函數進行簡化,最終返回惟一一個元素,此操做不會修改原始容器。
2. QtConcurrent::map
map的範例:http://doc.qt.io/qt-5/qtconcurrent-map-example.html函數
map詳細介紹:http://doc.qt.io/qt-5/qtconcurrentmap.htmlpost
- #include <QCoreApplication>
- #include <QtConcurrent>
- #include <QVector>
- #include <QDebug>
- #include <QFuture>
- void MapFunction(int& num) {
- num += 1;
- }
- int mappedReducedFunction(const int& num) {
- return num + 1;
- }
- void ReduceFunction(int& result, const int& item) {
- int t_r = result;
- result = item > result ? item : result;
- qDebug()<<t_r<<result<<item;
- }
- int main(int argc, char *argv[]) {
- QCoreApplication a(argc, argv);
- QVector<int> testVactor;
- for(int i = 1; i <= 3; i++) {
- testVactor.push_back(i);
- }
- for(int i = 1; i <= 3; i++) {
- testVactor.push_back(10-i);
- }
- qDebug() << "start m:" << testVactor;
- QFuture<void> f = QtConcurrent::map(testVactor, MapFunction);
- f.waitForFinished();
- qDebug() << "map result:" << testVactor;//map直接修改源數據
- QFuture<int> r = QtConcurrent::mappedReduced(testVactor, mappedReducedFunction, ReduceFunction);
- qDebug() << "mappedReduced result:" << r.result();
- return 0;
- }
注意幾個函數的聲明形式,不可有差距。結果spa
- start m: QVector(1, 2, 3, 9, 8, 7)
- map result: QVector(2, 3, 4, 10, 9, 8)
- 0 3 3
- 3 4 4
- 4 5 5
- 5 11 11
- 11 11 10
- 11 11 9
- mappedReduced result: 11
結果示意很明顯,reduced最終表留的是等於函數result參數值的項線程
3. QtConcurrent::filter
filter詳細介紹:http://doc.qt.io/qt-5/qtconcurrentfilter.htmlhtm
- #include <QCoreApplication>
- #include <QtConcurrent>
- #include <QList>
- #include <QDebug>
- #include <QFuture>
- bool filterFunction(const int& num) {
- return (num > 5);
- }
- void ReduceFunction(int& result, const int& item) {
- int t_r = result;
- result = item > result ? item : result;
- qDebug()<<t_r<<result<<item;
- }
- int main(int argc, char *argv[]) {
- QCoreApplication a(argc, argv);
- QList<int> testVactor;
- for(int i = 1; i <= 3; i++) {
- testVactor.push_back(i);
- }
- for(int i = 1; i <= 3; i++) {
- testVactor.push_back(10-i);
- }
- qDebug() << "start m:" << testVactor;
- QFuture<void> f = QtConcurrent::filter(testVactor, filterFunction);
- f.waitForFinished();
- qDebug() << "map result:" << testVactor;//map直接修改源數據
- QFuture<int> r = QtConcurrent::filteredReduced(testVactor, filterFunction, ReduceFunction);
- qDebug() << "mappedReduced result:" << r.result();
- return 0;
- }
注意幾個函數的聲明形式,不可有差距。filter函數要返回bool類型,用於判斷是否過濾此元素接口
結果:
- start m: (1, 2, 3, 9, 8, 7)
- map result: (9, 8, 7)
- 0 9 9
- 9 9 8
- 9 9 7
- mappedReduced result: 9
4. QtConcurrent::run
感受run用起來很舒服,由於他沒有對運行函數頭作限制,能夠是任意數量的任意類型參數。
run的詳細幫助:http://doc.qt.io/qt-5/qtconcurrentrun.html,也能夠看看本機的qtconcurrentrun.h文件,能夠看到裏面有不少的run的重載函數
下面給出最基本的使用
- #include <QCoreApplication>
- #include <QtConcurrent>
- #include <QList>
- #include <QDebug>
- #include <QThread>
- void function(const QList<int>& param1, const int& param2, Qt::HANDLE main_id) {
- qDebug()<<"function param:"<<param1<<param2<<main_id;
- qDebug()<<"function thread id:" <<QThread::currentThreadId();
- }
- int main(int argc, char *argv[]) {
- QCoreApplication a(argc, argv);
- QList<int> testVactor;
- for(int i = 1; i <= 3; i++) {
- testVactor.push_back(i);
- }
- qDebug() << "main thread id:" << QThread::currentThreadId();
- QFuture<void> f = QtConcurrent::run(function,testVactor,666,QThread::currentThreadId());
- f.waitForFinished();//要等待,不然線程沒運行完程序結束會出錯
- return 0;
- }
結果
- main thread id: 0x2a10
- function param: (1, 2, 3) 666 0x2a10
- function thread id: 0x2344
4.1. 其餘使用方式-指定線程池
有時候但願運行的函數在全局線程池或者局部線程池運行,而不是有qt託管處理,能夠進行以下方式調用:
- extern void aFunction();
- QThreadPool pool;
- QFuture<void> future = QtConcurrent::run(&pool, aFunction);
5. 阻塞QtConcurrent
上述全部函數都是非阻塞的,因此在return 0前都有waitForFinished,qt一樣提供了阻塞函數
見最開始API幫助介紹鏈接,能夠看到相關接口
- void blockingFilter(Sequence &sequence, FilterFunction filterFunction)
- Sequence blockingFiltered(const Sequence &sequence, FilterFunction filterFunction)
- Sequence blockingFiltered(ConstIterator begin, ConstIterator end, FilterFunction filterFunction)
- T blockingFilteredReduced(const Sequence &sequence, FilterFunction filterFunction, ReduceFunction reduceFunction, QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce)
- T blockingFilteredReduced(ConstIterator begin, ConstIterator end, FilterFunction filterFunction, ReduceFunction reduceFunction, QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce)
- void blockingMap(Sequence &sequence, MapFunction function)
- void blockingMap(Iterator begin, Iterator end, MapFunction function)
- T blockingMapped(const Sequence &sequence, MapFunction function)
- T blockingMapped(ConstIterator begin, ConstIterator end, MapFunction function)
- T blockingMappedReduced(const Sequence &sequence, MapFunction mapFunction, ReduceFunction reduceFunction, QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce)
- T blockingMappedReduced(ConstIterator begin, ConstIterator end, MapFunction mapFunction, ReduceFunction reduceFunction, QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce)
能夠看到對應函數的介紹都有:
Note: This function will block until all items in the sequence have been processed.
使用方式近似,不提供示例了。