Qt多线程-QtConcurrent并行运算高级API

博客地址已更改,文章数量较多不便批量修改,若想访问源文请到 coologic博客 查阅,网址:www.coologic.cn

如本文记录地址为 techieliang.com/A/B/C/ 请改为 www.coologic.cn/A/B/C/ 即可查阅

 

版权声明:若无来源注明,Techie亮博客文章均为原创。 转载请以链接形式标明本文标题和地址:
本文标题:Qt多线程-QtConcurrent并行运算高级API     本文地址:http://techieliang.com/2017/12/608/

1. 介绍

Qt除了提供基本的QThread实现多线程,并提供QThreadPool实现线程池以外,还提供了QtConcurrent模块用于并行计算。

使用此类需要在pro文件增加QT += concurrent

QtConcurrent命名空间帮助文档API介绍帮助

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, ...)

很多都是重载的,主要函数如下:

  • 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.html

  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. }

注意几个函数的声明形式,不可有差距。结果

  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.html

  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
posted @ 2017-12-10 22:02  Coologic  阅读(8899)  评论(0编辑  收藏  举报