Qt 提供用於處理綫程的很多類和函數。Qt 程序員可以使用以下 4 種不同方式以實現多綫程應用程序。
QThread 是 Qt 中所有綫程控製的基礎。每個 QThread 實例錶示並控製一綫程。
QThread 可以被直接實例化或子類化。實例化 QThread 提供並行事件循環,允許 QObject 槽在第 2 綫程中被援引。子類化 QThread 允許應用程序初始化新綫程在開始其事件循環之前,或運行沒有事件循環的並行代碼。
見 QThread 類參考 和 綫程範例 演示如何使用 QThread .
Creating and destroying threads frequently can be expensive. To reduce this overhead, existing threads can be reused for new tasks. QThreadPool is a collection of reuseable QThreads.
To run code in one of a QThreadPool 's threads, reimplement QRunnable::run () and instantiate the subclassed QRunnable 。使用 QThreadPool::start () to put the QRunnable 在 QThreadPool 's run queue. When a thread becomes available, the code within QRunnable::run () will execute in that thread.
Each Qt application has a global thread pool, which is accessible through QThreadPool::globalInstance (). This global thread pool automatically maintains an optimal number of threads based on the number of cores in the CPU. However, a separate QThreadPool can be created and managed explicitly.
The Qt Concurrent module provides high-level functions that deal with some common parallel computation patterns: map, filter, and reduce. Unlike using QThread and QRunnable , these functions never require the use of low-level threading primitives such as mutexes or semaphores. Instead, they return a QFuture object which can be used to retrieve the functions' results when they are ready. QFuture can also be used to query computation progress and to pause/resume/cancel the computation. For convenience, QFutureWatcher enables interactions with QFuture s via signals and slots.
Qt Concurrent 's map, filter and reduce algorithms automatically distribute computation across all available processor cores, so applications written today will continue to scale when deployed later on a system with more cores.
This module also provides the QtConcurrent::run () function, which can run any function in another thread. However, QtConcurrent::run () only supports a subset of features available to the map, filter and reduce functions. The QFuture can be used to retrieve the function's return value and to check if the thread is running. However, a call to QtConcurrent::run () uses one thread only, cannot be paused/resumed/canceled, and cannot be queried for progress.
見 Qt Concurrent module documentation for details on the individual functions.
The WorkerScript QML type lets JavaScript code run in parallel with the GUI thread.
每個
WorkerScript
instance can have one
.js
script attached to it. When
WorkerScript::sendMessage
() is called, the script will run in a separate thread (and a separate
QML context
). When the script finishes running, it can send a reply back to the GUI thread which will invoke the WorkerScript::onMessage() signal handler.
使用 WorkerScript is similar to using a worker QObject that has been moved to another thread. Data is transferred between threads via signals.
見 WorkerScript documentation for details on how to implement the script, and for a list of data types that can be passed between threads.
As demonstrated above, Qt provides different solutions for developing threaded applications. The right solution for a given application depends on the purpose of the new thread and the thread's lifetime. Below is a comparison of Qt's threading technologies, followed by recommended solutions for some example use cases.
| 特徵 | QThread | QRunnable and QThreadPool | QtConcurrent::run () | Qt Concurrent (Map, Filter, Reduce) | WorkerScript |
|---|---|---|---|---|---|
| Language | C++ | C++ | C++ | C++ | QML |
| Thread priority can be specified | Yes | Yes | |||
| Thread can run an event loop | Yes | ||||
| Thread can receive data updates through signals | Yes (received by a worker QObject ) | Yes (received by WorkerScript ) | |||
| Thread can be controlled using signals | Yes (received by QThread ) | Yes (received by QFutureWatcher ) | |||
| Thread can be monitored through a QFuture | Partially | Yes | |||
| Built-in ability to pause/resume/cancel | Yes |
| 綫程壽命 | 操作 | Solution |
|---|---|---|
| One call | Run a new linear function within another thread, optionally with progress updates during the run. |
Qt 提供不同解決方案:
|
| One call | Run an existing function within another thread and get its return value. | Run the function using QtConcurrent::run (). Have a QFutureWatcher emit the finished() signal when the function has returned, and call QFutureWatcher::result () to get the function's return value. |
| One call | Perform an operation on all items of a container, using all available cores. For example, producing thumbnails from a list of images. | Use Qt Concurrent's QtConcurrent::filter () function to select container elements, and the QtConcurrent::map () function to apply an operation to each element. To fold the output into a single result, use QtConcurrent::filteredReduced () 和 QtConcurrent::mappedReduced () 代替。 |
| One call/Permanent | Perfrom a long computation in a pure QML application, and update the GUI when the results are ready. |
Place the computation code in a
.js
script and attach it to a
WorkerScript
instance. Call
sendMessage()
to start the computation in a new thread. Let the script call
WorkerScript::sendMessage
() too, to pass the result back to the GUI thread. Handle the result in
onMessage
and update the GUI there.
|
| Permanent | Have an object living in another thread that can perform different tasks upon request and/or can receive new data to work with. | Subclass a QObject to create a worker. Instantiate this worker object and a QThread . Move the worker to the new thread. Send commands or data to the worker object over queued signal-slot connections. |
| Permanent | Repeatedly perform an expensive operation in another thread, where the thread does not need to receive any signals or events. | Write the infinite loop directly within a reimplementation of QThread::run (). Start the thread without an event loop. Let the thread emit signals to send data back to the GUI thread. |