Qt Multi Thread Signal Slot
Connect(pRtrThread, SIGNAL(displayInfo(QString)), pFacade-muiMainWindow.outputPane, SLOT(append(QString)), Qt::QueuedConnection); connect(pRtrThread, SIGNAL(taskOver), CFacadeHandler::getUniqueInstance(pFacade-muiMainWindow), SLOT(changeCursorShape), Qt::QueuedConnection). Signals & Slots, Signals and slots are made possible by Qt's meta-object system. To one signal, the slots will be executed one after the other, in the order they have valueChanged, and it has a slot which other objects can send signals to. The context object provides information about in which thread the receiver should be executed.
how in BOOST send a signal in a thread and have the corresponding slot executed in another thread?
boost::signals2::signal
boost signal handler
boost signal multi-threaded
boost multithreading
In Qt for instance if you emit a signal in a thread other that the GUI thread, the signal is enqueued and executed later in the GUI thread, is there a way to do that with boost?
thanks
For an event loop use boost::asio::io_service. You can post tasks inside this object and have another thread execute them, in a thread safe way:
Messaging and Signaling in C++, But as this blog post is more on signaling then system events. Qt signal/slot implementation is thread safe, so that you can use it to send messages important, as anything UI related should run in the main thread of Qt, anything that I use this in a different program to have one widget for editing flag like Almost all classes provided by Boost.Signals2 are thread safe and can be used in multithreaded applications. For example, objects of type boost::signals2::signal and boost::signals2::connection can be accessed from different threads. On the other hand, boost::signals2::shared_connection_block is not thread safe.
Not directly, because boost does not provide an event loop.
To have a signal handled in another thread, that another thread needs to be checking the queue of handlers it should run and execute them (which usually means some kind of event-loop). Boost does not provide one, so you'll need to get it from elsewhere or write it.
If you have an event-loop, that does not provide signals, (or implement some simple solution with queues) you should be able to (ab)use boost.signals2 (not boost.signals, because that version is not thread-safe) by overriding the operator+=
to wrap each handler in something, that will queue it for execution in the other thread. You might even be able to implement it for signals with return values (which is not supported by Qt, but is supported by boost), but you'll have to be careful to avoid dead-lock.
[PDF] Boost.Signals2, Signals2 library is an implementation of a managed signals and slots system. This documentation describes a thread-safe variant of the original Boost. so we put 'Hello' into a group that must be executed before the group possible to set up tracking in a post-constructor which is called after the object has been created To have a signal handled in another thread, that another thread needs to be checking the queue of handlers it should run and execute them (which usually means some kind of event-loop). Boost does not provide one, so you'll need to get it from elsewhere or write it.
Signals & Slots, Signals and slots are made possible by Qt's meta-object system. to one signal, the slots will be executed one after the other, in the order they have valueChanged() , and it has a slot which other objects can send signals to. The context object provides information about in which thread the receiver should be executed. Special behavior for C++: If a thread is sent a signal using pthread_kill() and that thread does not handle the signal, then destructors for local objects may not be executed. Usage notes. The SIGTHSTOP and SIGTHCONT signals can be issued by this function. pthread_kill() is the only function that can issue SIGTHSTOP or SIGTHCONT. Returned value
Chila's answer is correct, but it's missing one important thing:A boost::thread
object will only call the function its passed once. Since the boost::io_service
has no work to do until the signal is emitted, the thread will finish immediately. To counter this there is a boost::asio::io_service::work
class.Before you call the run()
method of the io_service
you should create a work object and pass it the io_service
:
Note: At the time of writing (boost 1.67) this method is already deprecated and you are supposed to use io_context::executor_work_guard
(basically same functionality as io_service::work
). I was not able to compile when using the new method though, and the work solution is still working in boost 1.67.
Slots, It also implements a few conditional (event) related classes. Qt - SigSlot - Boost Libraries Qt was the original signal/slots implementation, but it Sigslot and Boost on the other hand are pure ISO C++, but both have some disadvantages. None of these are thread-safe and it can be somewhat inconvenient manually Qt documentation states that signals and slots can be direct, queued and auto. It also stated that if object that owns slot 'lives' in a thread different from object that owns signal, emitting such signal will be like posting message - signal emit will return instantly and slot method will be called in target thread's event loop.
For some reason, the assignment operator of boost::asio::executor_work_guard<boost::asio::io_context::executor_type>
is deleted, but you still can construct it.
Here's my version of the code that posts some movable Event
object and processes it on the thread running io_context::run()
:
It requires C++14 and was tested with VS2017 and GCC 6.4 with thread & memory sanitizers.
Observer pattern with Stl, boost and qt, A comparison between the Qt signal and slot mechanism and some Each slot is a potential callback ○ Adds more run-time introspection, The Synapse library ○ Another signals/slot like library ○ Submitted to Very similar to boost::signals2 ○ Have the ability to transfer control between threads 29; 30. So, when the thread is created from the create_thread method it will call the io_service::run method and it passes the io_service object as an argument. Typically one io_service object can be used with multiple socket objects.
QThreads: Are You Using Them Wrong?, Show related SlideShares at end The Basics of QThread QThread manages one thread of execution ○ The Signal Slot Connections and Threads ○ Qt::DirectConnection have same thread affinity: Direct ○ If objects have different thread It implies you want to send cross-thread signals to yourself. Direct Connection The slot is invoked immediately, when the signal is emitted. The slot is executed in the emitter's thread, which is not necessarily the receiver's thread. Queued Connection The slot is invoked when control returns to the event loop of the receiver's thread. The slot is executed in the receiver's thread.
What do I do if a slot is not invoked?, A practical checklist to debug your signal/slot connections that an event loop is running in the thread the receiver has affinity with;; that all the arguments Using this signal is very easy – it just acts like a flag, but you can wait for it as well as read it. The following unit test (also in GitHub) shows how the signal makes it easy for threads to set gates on each other. Note that the final signal in this example could have been done with thread.join(), but wasn’t for the purposes of the test.
[Boost-users] Signals2 benchmark, I want to test it against boost::signals2 to get an idea of how well it performs. Suppose one thread disconnects a slot while another fires a signal Each Signal-type has its own corresponding vector of slots defined within the Emitter. the copy being made instead of every slot being called and executed. POSIX requires that signal is thread-safe, and specifies a list of async-signal-safe library functions that may be called from any signal handler. Signal handlers are expected to have C linkage and, in general, only use the features from the common subset of C and C++. It is implementation-defined if a function with C++ linkage can be used as a
Comments
- THX for this very helpfull sample ! Since boost::signal is deprecated I have to use boost::signals2::signal<>.
Hot Questions
What Are Threads?
Threads are about doing things in parallel, just like processes. So how do threads differ from processes? While you are making calculations on a spreadsheet, there may also be a media player running on the same desktop playing your favorite song. Here is an example of two processes working in parallel: one running the spreadsheet program; one running a media player. Multitasking is a well known term for this. A closer look at the media player reveals that there are again things going on in parallel within one single process. While the media player is sending music to the audio driver, the user interface with all its bells and whistles is being constantly updated. This is what threads are for — concurrency within one single process.
So how is concurrency implemented? Parallel work on single core CPUs is an illusion which is somewhat similar to the illusion of moving images in cinema. For processes, the illusion is produced by interrupting the processor's work on one process after a very short time. Then the processor moves on to the next process. In order to switch between processes, the current program counter is saved and the next processor's program counter is loaded. This is not sufficient because the same needs to be done with registers and certain architecture and OS specific data.
Just as one CPU can power two or more processes, it is also possible to let the CPU run on two different code segments of one single process. When a process starts, it always executes one code segment and therefore the process is said to have one thread. However, the program may decide to start a second thread. Then, two different code sequences are processed simultaneously inside one process. Concurrency is achieved on single core CPUs by repeatedly saving program counters and registers then loading the next thread's program counters and registers. No cooperation from the program is required to cycle between the active threads. A thread may be in any state when the switch to the next thread occurs.
The current trend in CPU design is to have several cores. A typical single-threaded application can make use of only one core. However, a program with multiple threads can be assigned to multiple cores, making things happen in a truly concurrent way. As a result, distributing work to more than one thread can make a program run much faster on multicore CPUs because additional cores can be used.
GUI Thread and Worker Thread
As mentioned, each program has one thread when it is started. This thread is called the 'main thread' (also known as the 'GUI thread' in Qt applications). The Qt GUI must run in this thread. All widgets and several related classes, for example QPixmap, don't work in secondary threads. A secondary thread is commonly referred to as a 'worker thread' because it is used to offload processing work from the main thread.
Simultaneous Access to Data
Each thread has its own stack, which means each thread has its own call history and local variables. Unlike processes, threads share the same address space. The following diagram shows how the building blocks of threads are located in memory. Program counter and registers of inactive threads are typically kept in kernel space. There is a shared copy of the code and a separate stack for each thread.
If two threads have a pointer to the same object, it is possible that both threads will access that object at the same time and this can potentially destroy the object's integrity. It's easy to imagine the many things that can go wrong when two methods of the same object are executed simultaneously.
Sometimes it is necessary to access one object from different threads; for example, when objects living in different threads need to communicate. Since threads use the same address space, it is easier and faster for threads to exchange data than it is for processes. Data does not have to be serialized and copied. Passing pointers is possible, but there must be a strict coordination of what thread touches which object. Simultaneous execution of operations on one object must be prevented. There are several ways of achieving this and some of them are described below.
So what can be done safely? All objects created in a thread can be used safely within that thread provided that other threads don't have references to them and objects don't have implicit coupling with other threads. Such implicit coupling may happen when data is shared between instances as with static members, singletons or global data. Familiarize yourself with the concept of thread safe and reentrant classes and functions.
Using Threads
There are basically two use cases for threads:
- Make processing faster by making use of multicore processors.
- Keep the GUI thread or other time critical threads responsive by offloading long lasting processing or blocking calls to other threads.
When to Use Alternatives to Threads
Developers need to be very careful with threads. It is easy to start other threads, but very hard to ensure that all shared data remains consistent. Problems are often hard to find because they may only show up once in a while or only on specific hardware configurations. Before creating threads to solve certain problems, possible alternatives should be considered.
Alternative | Comment |
---|---|
QEventLoop::processEvents() | Calling QEventLoop::processEvents() repeatedly during a time-consuming calculation prevents GUI blocking. However, this solution doesn't scale well because the call to processEvents() may occur too often, or not often enough, depending on hardware. |
QTimer | Background processing can sometimes be done conveniently using a timer to schedule execution of a slot at some point in the future. A timer with an interval of 0 will time out as soon as there are no more events to process. |
QSocketNotifierQNetworkAccessManagerQIODevice::readyRead() | This is an alternative to having one or multiple threads, each with a blocking read on a slow network connection. As long as the calculation in response to a chunk of network data can be executed quickly, this reactive design is better than synchronous waiting in threads. Reactive design is less error prone and energy efficient than threading. In many cases there are also performance benefits. |
In general, it is recommended to only use safe and tested paths and to avoid introducing ad-hoc threading concepts. QtConcurrent provides an easy interface for distributing work to all of the processor's cores. The threading code is completely hidden in the QtConcurrent framework, so you don't have to take care of the details. However, QtConcurrent can't be used when communication with the running thread is needed, and it shouldn't be used to handle blocking operations.
Which Qt Thread Technology Should You Use?
Sometimes you want to do more than just running a method in the context of another thread. You may want to have an object which lives in another thread that provides a service to the GUI thread. Maybe you want another thread to stay alive forever to poll hardware ports and send a signal to the GUI thread when something noteworthy has happened. Qt provides different solutions for developing threaded applications. The right solution depends on the purpose of the new thread as well as on the thread's lifetime.
Lifetime of thread | Development task | Solution |
---|---|---|
One call | Run one method within another thread and quit the thread when the method is finished. | Qt provides different solutions:
|
One call | Operations are to be performed on all items of a container. Processing should be performed using all available cores. A common example is to produce thumbnails from a list of images. | QtConcurrent provides the map() function for applying operations on every container element, filter() for selecting container elements, and the option of specifying a reduce function for combining the remaining elements. |
One call | A long running operation has to be put in another thread. During the course of processing, status information should be sent to the GUI thread. | Use QThread, reimplement run and emit signals as needed. Connect the signals to the GUI thread's slots using queued signal/slot connections. |
Permanent | Have an object living in another thread and let it perform different tasks upon request. This means communication to and from the worker thread is required. | Derive a class from QObject and implement the necessary slots and signals, move the object to a thread with a running event loop and communicate with the object over queued signal/slot connections. |
Permanent | Have an object living in another thread, let the object perform repeated tasks such as polling a port and enable communication with the GUI thread. | Same as above but also use a timer in the worker thread to implement polling. However, the best solution for polling is to avoid it completely. Sometimes using QSocketNotifier is an alternative. |
Qt Thread Basics
QThread is a very convenient cross platform abstraction of native platform threads. Starting a thread is very simple. Let us look at a short piece of code that generates another thread which says hello in that thread and then exits.
We derive a class from QThread and reimplement the run() method.
The run method contains the code that will be run in a separate thread. In this example, a message containing the thread ID will be printed. QThread::start() will call the method in another thread.
To start the thread, our thread object needs to be instantiated. The start() method creates a new thread and calls the reimplemented run() method in this new thread. Right after start() is called, two program counters walk through the program code. The main function 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.
This is the result of running the code:
QObject and Threads
As mentioned above, developers must always be careful when calling objects' methods from other threads. Thread affinity does not change this situation. Qt documentation marks several methods as thread-safe. postEvent() is a noteworthy example. A thread-safe method may be called from different threads simultaneously.
In cases where there is usually no concurrent access to methods, calling non-thread-safe methods of objects in other threads may work thousands of times before a concurrent access occurs, causing unexpected behavior. Writing test code does not entirely ensure thread correctness, but it is still important. On Linux, Valgrind and Helgrind can help detect threading errors.
Using a Mutex to Protect the Integrity of Data
A mutex is an object that has lock() and unlock() methods and remembers if it is already locked. A mutex is designed to be called from multiple threads. lock() returns immediately if the mutex is not locked. The next call from another thread will find the mutex in a locked state and then lock() will block the thread until the other thread calls unlock(). This functionality can make sure that a code section will be executed by only one thread at a time.
The following line sketches how a mutex can be used to make a method thread-safe:
What happens if one thread does not unlock a mutex? The result can be a frozen application. In the example above, an exception might be thrown and mutex.unlock()
will never be reached. To prevent problems like this, QMutexLocker should be used.
This looks easy, but mutexes introduce a new class of problems: deadlocks. A deadlock happens when a thread waits for a mutex to become unlocked, but the mutex remains locked because the owning thread is waiting for the first thread to unlock it. The result is a frozen application. Mutexes can be used to make a method thread safe. Most Qt methods aren't thread safe because there is always a performance penalty when using mutexes.
It isn't always possible to lock and unlock a mutex in a method. Sometimes the need to lock spans several calls. For example, modifying a container with an iterator requires a sequence of several calls which should not be interrupted by other threads. In such a scenario, locking can be achieved with a mutex that is kept outside of the object to be manipulated. With an external mutex, the duration of locking can be adjusted to the needs of the operation. One disadvantage is that external mutexes aid locking, but do not enforce it because users of the object may forget to use it.
Using the Event Loop to Prevent Data Corruption
The event loops of Qt are a very valuable tool for inter-thread communication. Every thread may have its own event loop. A safe way of calling a slot in another thread is by placing that call in another thread's event loop. This ensures that the target object finishes the method that is currently running before another method is started.
So how is it possible to put a method invocation in an event loop? Qt has two ways of doing this. One way is via queued signal-slot connections; the other way is to post an event with QCoreApplication::postEvent(). A queued signal-slot connection is a signal slot connection that is executed asynchronously. The internal implementation is based on posted events. The arguments of the signal are put into the event loop and the signal method returns immediately.
Qt Signal Slot Thread
The connected slot will be executed at a time which depends on what else is in the event loop.
Communication via the event loop eliminates the deadlock problem we face when using mutexes. This is why we recommend using the event loop rather than locking an object using a mutex.
Dealing with Asynchronous Execution
One way to obtain a worker thread's result is by waiting for the thread to terminate. In many cases, however, a blocking wait isn't acceptable. The alternative to a blocking wait are asynchronous result deliveries with either posted events or queued signals and slots. This generates a certain overhead because an operation's result does not appear on the next source line, but in a slot located somewhere else in the source file. Qt developers are used to working with this kind of asynchronous behavior because it is much similar to the kind of event-driven programming used in GUI applications.
Examples
This tutorial comes with examples for Qt's three basic ways of working with threads. Two more examples show how to communicate with a running thread and how a QObject can be placed in another thread, providing service to the main thread.
- Using QThread as shown above
The following examples can all be compiled and run independently. The source can be found in the examples directory: examples/tutorials/threads/
Example 1: Using the Thread Pool
Creating and destroying threads frequently can be expensive. To avoid the cost of thread creation, a thread pool can be used. A thread pool is a place where threads can be parked and fetched. We can write the same 'hello thread' program as above using the global thread pool. We derive a class from QRunnable. The code we want to run in another thread needs to be placed in the reimplemented QRunnable::run() method.
We instantiate Work in main(), locate the global thread pool and use the QThreadPool::start() method. Now the thread pool runs our worker in another thread. Using the thread pool has a performance advantage because threads are not destroyed after they have finished running. They are kept in a pool and wait to be used again later.
Example 2: Using QtConcurrent
We write a global function hello() to implement the work. QtConcurrent::run() is used to run the function in another thread. The result is a QFuture. QFuture provides a method called waitForFinished(), which blocks until the calculation is completed. The real power of QtConcurrent becomes visible when data can be made available in a container. QtConcurrent provides several functions that are able to process itemized data on all available cores simultaneously. The use of QtConcurrent is very similar to applying an STL algorithm to an STL container. QtConcurrent Map is a very short and clear example about how a container of images can be scaled on all available cores. The image scaling example uses the blocking variants of the functions used. For every blocking function there is also a non-blocking, asynchronous counterpart. Getting results asynchronously is implemented with QFuture and QFutureWatcher.
Example 3: Clock
We want to produce a clock application. The application has a GUI and a worker thread. The worker thread checks every 10 milliseconds what time it is. If the formatted time has changed, the result will be sent to the GUI thread where it is displayed.
Of course, this is an overly complicated way of designing a clock and, actually, a separate thread is unnecessary. We would be better off placing the timer in the main thread because the calculation made in the timer slot is very short-lived. This example is purely for instructional use and shows how to communicate from a worker thread to a GUI thread. Note that communication in this direction is easy. We only need to add a signal to QThread and make a queued signal/slot connection to the main thread. Communication from the GUI to the worker thread is shown in the next example.
We've connected the clockThread
with the label. The connection must be a queued signal-slot connection because we want to put the call in the event loop.
We have derived a class from QThread and declared the sendTime()
signal.
The trickiest part of this example is that the timer is connected to its slot via a direct connection. A default connection would produce a queued signal-slot connection because the connected objects live in different threads; remember that QThread does not live in the thread it creates.
Still it is safe to access ClockThread::timerHit() from the worker thread because ClockThread::timerHit() is private and only touches local variables and a private member that isn't touched by public methods. QDateTime::currentDateTime() isn't marked as thread-safe in Qt documentation, however we can get away with using it in this small example because we know that the QDateTime::currentDateTime() static method isn't used in any other threads.
Example 4: A Permanent Thread
This example shows how it is possible to have a QObject in a worker thread that accepts requests from the GUI thread, does polling using a timer and continuously reports results back to the GUI thread. The actual work including the polling must be implemented in a class derived from QObject. We have called this class WorkerObject
in the code shown below. The thread-specific code is hidden in a class called Thread
, derived from QThread. Thread
has two additional public members. The launchWorker()
member takes the worker object and moves it to another thread with a started event loop. The call blocks for a very short moment until the thread creation operation is completed, allowing the worker object to be used again on the next line. The Thread
class's code is short but somewhat involved, so we only show how to use the class.
QMetaObject::invokeMethod() calls a slot via the event loop. The worker object's methods should not be called directly after the object has been moved to another thread. We let the worker thread do some work and polling, and use a timer to shut the application down after 3 seconds. Shutting the worker down needs some care. We call Thread::stop()
to exit the event loop. We wait for the thread to terminate and, after this has occurred, we delete the worker.
Digging Deeper
Threading is a very complicated subject. Qt offers more classes for threading than we have presented in this tutorial. The following materials can help you go into the subject in more depth:
- Good video tutorials about threads with Qt can be found in the material from the Training Day at Qt Developer Days 2009.
- The Thread Support in Qt document is a good starting point into the reference documentation.
- Qt comes with several additional examples for QThread and QtConcurrent.
- Several good books describe how to work with Qt threads. The most extensive coverage can be found in Advanced Qt Programming by Mark Summerfield, Prentice Hall - roughly 70 of 500 pages cover QThread and QtConcurrent.
Qt Signal Slot Not Working
© 2016 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.