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 .
使用
find_package()
命令定位所需模塊組件在
Qt6
包:
find_package(Qt6 REQUIRED COMPONENTS Concurrent) target_link_libraries(mytarget PRIVATE Qt6::Concurrent)
另請參閱 構建采用 CMake 概述。
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
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 許可 進一步瞭解細節。