Qt Call Slot From Another Thread
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.
- Qt Execute Slot In Another Thread
- Qt Call Slot From Another Thread Set
- Qt Call Slot In Different Thread
- Qt Call Slot From Another Thread Pattern
While the purpose of threads is to allow code to run in parallel, there are times where threads must stop and wait for other threads. For example, if two threads try to write to the same variable simultaneously, the result is undefined. The principle of forcing threads to wait for one another is called mutual exclusion. It is a common technique for protecting shared resources such as data.
Qt provides low-level primitives as well as high-level mechanisms for synchronizing threads.
Low-Level Synchronization Primitives
QMutex is the basic class for enforcing mutual exclusion. A thread locks a mutex in order to gain access to a shared resource. If a second thread tries to lock the mutex while it is already locked, the second thread will be put to sleep until the first thread completes its task and unlocks the mutex.
QReadWriteLock is similar to QMutex, except that it distinguishes between 'read' and 'write' access. When a piece of data is not being written to, it is safe for multiple threads to read from it simultaneously. A QMutex forces multiple readers to take turns to read shared data, but a QReadWriteLock allows simultaneous reading, thus improving parallelism.
QSemaphore is a generalization of QMutex that protects a certain number of identical resources. In contrast, a QMutex protects exactly one resource. The Semaphores Example shows a typical application of semaphores: synchronizing access to a circular buffer between a producer and a consumer.
- Whenever is star is drawn by the worker thread, it will emit a signal that is connected to the addImage slot. This slot is called with a QRect value, indicating where the star should be placed in the pixmap held by the viewer label, and an image of the star itself.
- The main starts with only the GUI thread running and it should terminate with only the GUI thread running. Exiting the program when another thread is still busy is a programming error, and therefore, wait is called which blocks the calling thread until the run method has completed.
QWaitCondition synchronizes threads not by enforcing mutual exclusion but by providing a condition variable. While the other primitives make threads wait until a resource is unlocked, QWaitCondition makes threads wait until a particular condition has been met. To allow the waiting threads to proceed, call wakeOne() to wake one randomly selected thread or wakeAll() to wake them all simultaneously. The Wait Conditions Example shows how to solve the producer-consumer problem using QWaitCondition instead of QSemaphore.
Note: Qt's synchronization classes rely on the use of properly aligned pointers. For instance, you cannot use packed classes with MSVC.
These synchronization classes can be used to make a method thread safe. However, doing so incurs a performance penalty, which is why most Qt methods are not made thread safe.
Risks
If a thread locks a resource but does not unlock it, the application may freeze because the resource will become permanently unavailable to other threads. This can happen, for example, if an exception is thrown and forces the current function to return without releasing its lock.
Another similar scenario is a deadlock. For example, suppose that thread A is waiting for thread B to unlock a resource. If thread B is also waiting for thread A to unlock a different resource, then both threads will end up waiting forever, so the application will freeze.
Convenience classes
QMutexLocker, QReadLocker and QWriteLocker are convenience classes that make it easier to use QMutex and QReadWriteLock. They lock a resource when they are constructed, and automatically unlock it when they are destroyed. They are designed to simplify code that use QMutex and QReadWriteLock, thus reducing the chances that a resource becomes permanently locked by accident.
High-Level Event Queues
Qt's event system is very useful for inter-thread communication. Every thread may have its own event loop. To call a slot (or any invokable method) in another thread, place that call in the target thread's event loop. This lets the target thread finish its current task before the slot starts running, while the original thread continues running in parallel.
Qt Execute Slot In Another Thread
To place an invocation in an event loop, make a queued signal-slot connection. Whenever the signal is emitted, its arguments will be recorded by the event system. The thread that the signal receiver lives in will then run the slot. Alternatively, call QMetaObject::invokeMethod() to achieve the same effect without signals. In both cases, a queued connection must be used because a direct connection bypasses the event system and runs the method immediately in the current thread.
Qt Call Slot From Another Thread Set
There is no risk of deadlocks when using the event system for thread synchronization, unlike using low-level primitives. However, the event system does not enforce mutual exclusion. If invokable methods access shared data, they must still be protected with low-level primitives.
Having said that, Qt's event system, along with implicitly shared data structures, offers an alternative to traditional thread locking. If signals and slots are used exclusively and no variables are shared between threads, a multithreaded program can do without low-level primitives altogether.
See also QThread::exec() and Threads and QObjects.
© 2020 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.
Qt offers many classes and functions for working with threads. Below are four different approaches that Qt programmers can use to implement multithreaded applications.
QThread: Low-Level API with Optional Event Loops
QThread is the foundation of all thread control in Qt. Each QThread instance represents and controls one thread.
QThread can either be instantiated directly or subclassed. Instantiating a QThread provides a parallel event loop, allowing QObject slots to be invoked in a secondary thread. Subclassing a QThread allows the application to initialize the new thread before starting its event loop, or to run parallel code without an event loop.
See the QThread class reference and the threading examples for demonstrations on how to use QThread.
QThreadPool and QRunnable: Reusing Threads
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. Use QThreadPool::start() to put the QRunnable in the 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.
Qt Concurrent: Using a High-level API
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 QFutures 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.
See the Qt Concurrent module documentation for details on the individual functions.
WorkerScript: Threading in QML
The WorkerScript QML type lets JavaScript code run in parallel with the GUI thread.
Each 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.
Using a WorkerScript is similar to using a worker QObject that has been moved to another thread. Data is transferred between threads via signals.
See the WorkerScript documentation for details on how to implement the script, and for a list of data types that can be passed between threads.
Choosing an Appropriate Approach
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.
Comparison of Solutions
Feature | 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 |
Example Use Cases
Qt Call Slot In Different Thread
Lifetime of thread | Operation | Solution |
---|---|---|
One call | Run a new linear function within another thread, optionally with progress updates during the run. | Qt provides different solutions:
|
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() and QtConcurrent::mappedReduced() instead. |
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 WorkerScript.sendMessage() to start the computation in a new thread. Let the script call 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. |
Qt Call Slot From Another Thread Pattern
© 2020 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.