Qt Concurrent

The Qt Concurrent module provides high-level APIs that make it possible to write multi-threaded programs without using low-level threading primitives such as mutexes, read-write locks, wait conditions, or semaphores. Programs written with Qt Concurrent automatically adjust the number of threads used according to the number of processor cores available. This means that applications written today will continue to scale when deployed on multi-core systems in the future.

Qt Concurrent includes functional programming style APIs for parallel list processing, including a MapReduce and FilterReduce implementation for shared-memory (non-distributed) systems, and classes for managing asynchronous computations in GUI applications:

Qt Concurrent 支持几种 STL 兼容容器和迭代器类型,但最适合处理拥有随机访问迭代器的 Qt 容器,例如 QList 。映射和过滤函数接受容器及开始/结束迭代器两者。

STL 迭代器支持概述:

迭代器类型 范例类 支持状态
输入迭代器 不支持
输出迭代器 不支持
转发迭代器 std::slist 支持
双向迭代器 std::list 支持
随机访问迭代器 QList , std::vector 支持并推荐

Random access iterators can be faster in cases where Qt Concurrent is iterating over a large number of lightweight items, since they allow skipping to any point in the container. In addition, using random access iterators allows Qt Concurrent to provide progress information through QFuture::progressValue () 和 QFutureWatcher::progressValueChanged ().

非原位修改函数 (譬如 mapped() 和 filtered()) 会拷贝容器,当调用时。若使用的是 STL 容器,此拷贝操作可能需要一些时间,在这种情况下,我们推荐指定开始和结束迭代器为代替容器。

使用模块

使用 Qt 模块要求直接或透过其它依赖链接到模块库。一些构建工具为此有贡献支持,包括 CMake and qmake .

构建采用 CMake

使用 find_package() 命令定位所需模块组件在 Qt6 包:

find_package(Qt6 REQUIRED COMPONENTS Concurrent)
target_link_libraries(mytarget PRIVATE Qt6::Concurrent)
					

另请参阅 构建采用 CMake 概述。

采用 qmake 构建

To configure the module for building with qmake, add the module as a value of the QT variable in the project's .pro file:

QT += concurrent
					

范例

Module Evolution

Qt Concurrent 的变化 lists important changes in the module API and functionality that were done for the Qt 6 series of Qt.

许可

Qt Concurrent 模块在商业许可下是可用的来自 Qt 公司 . In addition, it is available under free software licenses: The GNU LGPL (次一般公共许可) 第 3 版 ,或 GNU GPL (一般公共许可) 第 2 版 。见 Qt 许可 进一步了解细节。