Qt多線程-QtConcurrent並行運算高級API

版權聲明:若無來源註明, Techie亮博客文章均爲原創。 轉載請以連接形式標明本文標題和地址:
本文標題:Qt多線程-QtConcurrent並行運算高級API     本文地址: http://techieliang.com/2017/12/608/

1. 介紹

Qt除了提供基本的QThread實現多線程,並提供QThreadPool實現線程池之外,還提供了QtConcurrent模塊用於並行計算。html

使用此類須要在pro文件增長QT += concurrent多線程

QtConcurrent命名空間幫助文檔API介紹幫助app

1.1. API

  1. void blockingFilter(Sequence &sequence, FilterFunction filterFunction)
  2. Sequence blockingFiltered(const Sequence &sequence, FilterFunction filterFunction)
  3. Sequence blockingFiltered(ConstIterator begin, ConstIterator end, FilterFunction filterFunction)
  4. T blockingFilteredReduced(const Sequence &sequence, FilterFunction filterFunction, ReduceFunction reduceFunction, QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce)
  5. T blockingFilteredReduced(ConstIterator begin, ConstIterator end, FilterFunction filterFunction, ReduceFunction reduceFunction, QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce)
  6. void blockingMap(Sequence &sequence, MapFunction function)
  7. void blockingMap(Iterator begin, Iterator end, MapFunction function)
  8. T blockingMapped(const Sequence &sequence, MapFunction function)
  9. T blockingMapped(ConstIterator begin, ConstIterator end, MapFunction function)
  10. T blockingMappedReduced(const Sequence &sequence, MapFunction mapFunction, ReduceFunction reduceFunction, QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce)
  11. T blockingMappedReduced(ConstIterator begin, ConstIterator end, MapFunction mapFunction, ReduceFunction reduceFunction, QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce)
  12. QFuture<void> filter(Sequence &sequence, FilterFunction filterFunction)
  13. QFuture<T> filtered(const Sequence &sequence, FilterFunction filterFunction)
  14. QFuture<T> filtered(ConstIterator begin, ConstIterator end, FilterFunction filterFunction)
  15. QFuture<T> filteredReduced(const Sequence &sequence, FilterFunction filterFunction, ReduceFunction reduceFunction, QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce)
  16. QFuture<T> filteredReduced(ConstIterator begin, ConstIterator end, FilterFunction filterFunction, ReduceFunction reduceFunction, QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce)
  17. QFuture<void> map(Sequence &sequence, MapFunction function)
  18. QFuture<void> map(Iterator begin, Iterator end, MapFunction function)
  19. QFuture<T> mapped(const Sequence &sequence, MapFunction function)
  20. QFuture<T> mapped(ConstIterator begin, ConstIterator end, MapFunction function)
  21. QFuture<T> mappedReduced(const Sequence &sequence, MapFunction mapFunction, ReduceFunction reduceFunction, QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce)
  22. QFuture<T> mappedReduced(ConstIterator begin, ConstIterator end, MapFunction mapFunction, ReduceFunction reduceFunction, QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce)
  23. QFuture<T> run(Function function, ...)
  24. 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函數進行簡化,最終返回惟一一個元素,此操做不會修改原始容器。
  • Concurrent Filter and Filter-Reduce
    • QtConcurrent::filter() removes all items from a container based on the result of a filter function.
    • –從容器中刪除那些知足某個過來條件的項。
    • QtConcurrent::filtered() is like filter(), except that it returns a new container with the filtered results.
    • –功能相似於filter(),只不過它會返回一個包含剩餘元素的容器。
    • QtConcurrent::filteredReduced() is like filtered(), except that the filtered results are reduced or folded into a single result.
    • –功能相似於filtered(),後續進行reduce操做。
  • Concurrent Run
    • QtConcurrent::run() runs a function in another thread.
    • –用另外一個進程運行一個函數
  • QFuture represents the result of an asynchronous computation.
  • — 獲取一步計算結果
  • QFutureIterator allows iterating through results available via QFuture.
  • –經過使用QFuture容許遍歷結果
  • QFutureWatcher allows monitoring a QFuture using signals-and-slots.
  • — 利用信號槽監視QFuture
  • QFutureSynchronizer is a convenience class that automatically synchronizes several QFutures.
  • –自動同步多個futures

2. QtConcurrent::map

map的範例:http://doc.qt.io/qt-5/qtconcurrent-map-example.html函數

map詳細介紹:http://doc.qt.io/qt-5/qtconcurrentmap.htmlpost

  1. #include <QCoreApplication>
  2. #include <QtConcurrent>
  3. #include <QVector>
  4. #include <QDebug>
  5. #include <QFuture>
  6. void MapFunction(int& num) {
  7. num += 1;
  8. }
  9. int mappedReducedFunction(const int& num) {
  10. return num + 1;
  11. }
  12. void ReduceFunction(int& result, const int& item) {
  13. int t_r = result;
  14. result = item > result ? item : result;
  15. qDebug()<<t_r<<result<<item;
  16. }
  17. int main(int argc, char *argv[]) {
  18. QCoreApplication a(argc, argv);
  19. QVector<int> testVactor;
  20. for(int i = 1; i <= 3; i++) {
  21. testVactor.push_back(i);
  22. }
  23. for(int i = 1; i <= 3; i++) {
  24. testVactor.push_back(10-i);
  25. }
  26. qDebug() << "start m:" << testVactor;
  27. QFuture<void> f = QtConcurrent::map(testVactor, MapFunction);
  28. f.waitForFinished();
  29. qDebug() << "map result:" << testVactor;//map直接修改源數據
  30. QFuture<int> r = QtConcurrent::mappedReduced(testVactor, mappedReducedFunction, ReduceFunction);
  31. qDebug() << "mappedReduced result:" << r.result();
  32. return 0;
  33. }

注意幾個函數的聲明形式,不可有差距。結果spa

  1. start m: QVector(1, 2, 3, 9, 8, 7)
  2. map result: QVector(2, 3, 4, 10, 9, 8)
  3. 0 3 3
  4. 3 4 4
  5. 4 5 5
  6. 5 11 11
  7. 11 11 10
  8. 11 11 9
  9. mappedReduced result: 11

結果示意很明顯,reduced最終表留的是等於函數result參數值的項線程

3. QtConcurrent::filter

filter詳細介紹:http://doc.qt.io/qt-5/qtconcurrentfilter.htmlhtm

  1. #include <QCoreApplication>
  2. #include <QtConcurrent>
  3. #include <QList>
  4. #include <QDebug>
  5. #include <QFuture>
  6. bool filterFunction(const int& num) {
  7. return (num > 5);
  8. }
  9. void ReduceFunction(int& result, const int& item) {
  10. int t_r = result;
  11. result = item > result ? item : result;
  12. qDebug()<<t_r<<result<<item;
  13. }
  14. int main(int argc, char *argv[]) {
  15. QCoreApplication a(argc, argv);
  16. QList<int> testVactor;
  17. for(int i = 1; i <= 3; i++) {
  18. testVactor.push_back(i);
  19. }
  20. for(int i = 1; i <= 3; i++) {
  21. testVactor.push_back(10-i);
  22. }
  23. qDebug() << "start m:" << testVactor;
  24. QFuture<void> f = QtConcurrent::filter(testVactor, filterFunction);
  25. f.waitForFinished();
  26. qDebug() << "map result:" << testVactor;//map直接修改源數據
  27. QFuture<int> r = QtConcurrent::filteredReduced(testVactor, filterFunction, ReduceFunction);
  28. qDebug() << "mappedReduced result:" << r.result();
  29. return 0;
  30. }

注意幾個函數的聲明形式,不可有差距。filter函數要返回bool類型,用於判斷是否過濾此元素接口

結果:

  1. start m: (1, 2, 3, 9, 8, 7)
  2. map result: (9, 8, 7)
  3. 0 9 9
  4. 9 9 8
  5. 9 9 7
  6. mappedReduced result: 9

4. QtConcurrent::run

感受run用起來很舒服,由於他沒有對運行函數頭作限制,能夠是任意數量的任意類型參數。

run的詳細幫助:http://doc.qt.io/qt-5/qtconcurrentrun.html,也能夠看看本機的qtconcurrentrun.h文件,能夠看到裏面有不少的run的重載函數

下面給出最基本的使用

  1. #include <QCoreApplication>
  2. #include <QtConcurrent>
  3. #include <QList>
  4. #include <QDebug>
  5. #include <QThread>
  6. void function(const QList<int>& param1, const int& param2, Qt::HANDLE main_id) {
  7. qDebug()<<"function param:"<<param1<<param2<<main_id;
  8. qDebug()<<"function thread id:" <<QThread::currentThreadId();
  9. }
  10. int main(int argc, char *argv[]) {
  11. QCoreApplication a(argc, argv);
  12. QList<int> testVactor;
  13. for(int i = 1; i <= 3; i++) {
  14. testVactor.push_back(i);
  15. }
  16. qDebug() << "main thread id:" << QThread::currentThreadId();
  17. QFuture<void> f = QtConcurrent::run(function,testVactor,666,QThread::currentThreadId());
  18. f.waitForFinished();//要等待,不然線程沒運行完程序結束會出錯
  19. return 0;
  20. }

結果

  1. main thread id: 0x2a10
  2. function param: (1, 2, 3) 666 0x2a10
  3. function thread id: 0x2344

4.1. 其餘使用方式-指定線程池

有時候但願運行的函數在全局線程池或者局部線程池運行,而不是有qt託管處理,能夠進行以下方式調用:

  1. extern void aFunction();
  2. QThreadPool pool;
  3. QFuture<void> future = QtConcurrent::run(&pool, aFunction);

5. 阻塞QtConcurrent

上述全部函數都是非阻塞的,因此在return 0前都有waitForFinished,qt一樣提供了阻塞函數

見最開始API幫助介紹鏈接,能夠看到相關接口

  1. void blockingFilter(Sequence &sequence, FilterFunction filterFunction)
  2. Sequence blockingFiltered(const Sequence &sequence, FilterFunction filterFunction)
  3. Sequence blockingFiltered(ConstIterator begin, ConstIterator end, FilterFunction filterFunction)
  4. T blockingFilteredReduced(const Sequence &sequence, FilterFunction filterFunction, ReduceFunction reduceFunction, QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce)
  5. T blockingFilteredReduced(ConstIterator begin, ConstIterator end, FilterFunction filterFunction, ReduceFunction reduceFunction, QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce)
  6. void blockingMap(Sequence &sequence, MapFunction function)
  7. void blockingMap(Iterator begin, Iterator end, MapFunction function)
  8. T blockingMapped(const Sequence &sequence, MapFunction function)
  9. T blockingMapped(ConstIterator begin, ConstIterator end, MapFunction function)
  10. T blockingMappedReduced(const Sequence &sequence, MapFunction mapFunction, ReduceFunction reduceFunction, QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce)
  11. 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.

使用方式近似,不提供示例了。

轉載請以連接形式標明本文標題和地址: Techie亮博客 » Qt多線程-QtConcurrent並行運算高級API
相關文章
相關標籤/搜索