Should I use signal/slot as much as I can in Qt? - c++

From Qt documentation on Performance Considerations And Suggestions I got the following:
use asynchronous, event-driven programming wherever possible
I'm not sure what that means, so would like to ask. Does it mean I should use signal/slots whenever possible (because they are asynchronous?)?

Qt signals/slots are not necessarily asynchronous. From https://doc.qt.io/qt-5/threads-qobject.html:
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.
Blocking Queued Connection: The slot is invoked as for the Queued Connection, except the current thread blocks until the slot returns.
A signal subscribed to by a slot with a Direct Connection will be essentially a method call that you can "hook up" at runtime.
Also, yes, you should probably use "asynchronous, event-driven programming" "whenever possible" for a sane definition of "whenever possible".
Obviously, don't replace all method calls between your objects with signals and slots. And when you do use signals and slots, don't always make them asynchronous (Queued) - sometimes you will want the objects subscribed to your signals to finish their "reactions" to your signals before the emitting function proceeds.
In general, when you don't really care if the subscribers of your signals get their slots invoked immedtiatelly or later, just connect them up without specifying a connection type, and Qt will use Auto Connection, which will do the right thing (thread-wise). When you do care, just specify the type of connection you want.
If you feel confused by this at first, a reasonable thing to do might also be to make all connections Queued by default - you won't really notice any performance difference, and this might prevent you from accidentally writing code that depends on the slots executing "directly", when that was not your intent.
The suggestion in your link is mainly meant for any events that get generated on your main thread, most likely by UI elements - buttons, etc. The main idea is that you want to process any input events as quickly as possible, to keep the main thread free for accepting any later events and rendering your UI, and, if the events cause any significant work to be done, move that work to another thread, and have your main thread wait for a completion signal, so that your main thread remains "responsive". If you want your UI to immedtially react to any events, for example, by initiating a "loading spinner" or displaying a progress bar, you can, of course, do that directly. This, of course, also applies to any other threads that might need to remain responsive and handle other events while a larger calculation is happening in the background.

Related

qt: slot not called guarantee after disconnect of an auto direct connection

I have a single-thread gui application and I do not choose a type of connection, so according to documentation the connection will be a direct one. According to documentation then: "The slot is invoked immediately when the signal is emitted. The slot is executed in the signalling thread."
For my understanding, this obviously leads to the conclusion that if I disconnect such an auto-connection in a method, then there is a guarantee that there is no possibility that after completion of this method (in which I do the disconnection) I can get a slot (corresponding to the just-disconnected connection) still being called. The reason of that is, as far as I understand, the fact that no event queue is used for such an auto (here direct) connection.
Am I right? Or should I be careful and better keep a state-variable and double check this state and return from the slot if it's in a some (unclear to me) way still got called? I am especially worried for the situations where I have connections, in which signals are produced by Qt entities (e.g. signals QNetworkReply::finished(), etc.), rather than by me, as I do not control them fully.
I have found related questions, like this one, but I have not found the exact question, and so I have inconfidence and decided to ask.
If you use only direct connections then you can't "receive" any further signals after you disconnected from getting them. Direct connection is just a callback call, nothing more.

Solutions to replace Qt's signal-slot connections (across thread boundaries)?

In Qt one can:
connect(object, &Object::someSignal, objectInAnotherThread, &Object::someSlot);
So, when I connect a signal from an object in a thread to an object in another thread, Qt queues the signal and someSlot will be executed in the thread of objectInAnotherThread.
This particular feature is very handy and safe, although could copy data.
Lambdas in C++11 are handy, but when replacing this kind of connection with a pure lambda callback (without Qt), the lambda will be executed in the thread of the caller. This will then usually require mutexes etc error-prone logic to make things right.
I'm aware of Boost::signals2 etc, but AFAIK they don't provide this same Qt-like behavior when used across thread boundaries..?
If I'd like to remove Qt for a reason or another, what are my options for drop-in replacement regarding my signal-slot connections?
What’s wrong with spinning up a thread and sending wrapped function calls to a queue that the thread pulls from and executes? The event queue in Qt is not very special other than it uses the “native” event loop. There’s no need to do that, though, and e.g. QtConcurrent::run threads implement a simple mutex+wait condition protected queue. Whenever the new events are delivered, the thread gets woken up and processes them until the queue is empty. The events can carry functor calls. In fact, the events can simply be std::function. The only sticking point is timers, which you’d have to implement on top of the primitive that waits on the wait condition. Those waits have timeouts, and you’d use a sorted timeout queue and schedule wake ups whenever a timer object should “tick”. This has the benefit of not using up any native timers and can potentially perform better.

Is it safe to call a signal function of a widget class in Qt from multiple threads in C++?

Is it safe to call widget's signal function from multiple threads simultaneously? Will Qt use some kind of internal mutex to provide security of its own data structures when multiple threads call some widget's signal simultaneously?
As i understand, it is safe and N simultaneous calls of a signal function will lead to N sequential calls of a connected signal.
Am i correct?
P.S.
The threads that call a signal function are created with boost. I think, this is not important for this question. I cannot use another threads, because that threads are not related to GUI only, but they serve many parts of a program.
To be concise about this, you don't call a signal, you emit a signal. Then Qt internally handles the firing of any slots that the signal is connected to.
Emitting a signal and having it firing slot(s) may or may not be thread safe depending on the connection type.
Read here for more information.
And I think that the thread being created by boost will be a problem - the signal/slot mechanism relies on the infrastructure of QThread and QObject. It may be better and simpler if you can use QThread rather than a boost thread.
If you would be using QThreads, there would be no problem at all, as Qt manages such a situation automatically. So you (possible) problem arises from using boost threads.
However, there is a simple solution: make sure you connect the signals and slots using Qt::QueuedConnection. This will post an event into the Widget's thread's event loop and execute the slot in that thread. (this is what using QThreads does automatically when objects live in different threads).
Note that slot execution will be asynchronous in that case. If you need synchronous execution of slots (i.e. all slots must be finished before the code emitting the signal continues), use Qt::BlockingQueuedConnection
It depends on the connection. If the connection is direct, the emitting thread will be used to deliver the signal to connected slot. Is this safe? That depends on the slot. In particular, slots on QWidget cannot handle this. If the connection is queued, the signal is stored. The receiver object has an associated QThread, that thread has an event loop, and that event loop will deliver the stored signal to the receiver.
Your question describes the behavior of a queued connection. That's a valid possibility. The Qt event loop is thread safe. Both delivering signals to it, and calling slots from it are properly protected. Since there's only one QThread per receiver, that means queued signals are delivered sequentially.

Qt signals (QueuedConnection and DirectConnection)

I'm having trouble with Qt signals.
I don't understand how DirectConnection and QueuedConnection works?
I'd be thankful if someone will explain when to use which of these (sample code would be appreciated).
You won't see much of a difference unless you're working with objects having different thread affinities. Let's say you have QObjects A and B and they're both attached to different threads. A has a signal called somethingChanged() and B has a slot called handleChange().
If you use a direct connection
connect( A, SIGNAL(somethingChanged()), B, SLOT(handleChange()), Qt::DirectConnection );
the method handleChange() will actually run in the A's thread. Basically, it's as if emitting the signal calls the slot method "directly". If B::handleChange() isn't thread-safe, this can cause some (difficult to locate) bugs. At the very least, you're missing out on the benefits of the extra thread.
If you change the connection method to Qt::QueuedConnection (or, in this case, let Qt decide which method to use), things get more interesting. Assuming B's thread is running an event loop, emitting the signal will post an event to B's event loop. The event loop queues the event, and eventually invokes the slot method whenever control returns to it (it being the event loop). This makes it pretty easy to deal with communication between/among threads in Qt (again, assuming your threads are running their own local event loops). You don't have to worry about locks, etc. because the event loop serializes the slot invocations.
Note: If you don't know how to change a QObject's thread affinity, look into QObject::moveToThread. That should get you started.
Edit
I should clarify my opening sentence. It does make a difference if you specify a queued connection - even for two objects on the same thread. The event is still posted to the thread's event loop. So, the method call is still asynchronous, meaning it can be delayed in unpredictable ways (depending on any other events the loop may need to process). However, if you don't specify a connection method, the direct method is automatically used for connections between objects on the same thread (at least it is in Qt 4.8).
in addition to Jacob Robbins answer:
the statement "You won't see much of a difference unless you're working with objects having different thread affinities" is wrong;
emitting a signal to a direct connection within the same thread will execute the slot immediately, just like a simple function call.
emitting a signal to a queued connection within the same thread will enqueue the call into the threads event loop, thus the execution will always happen delayed.
QObject based class has a queued connection to itself
Jacob's answer is awesome. I'd just like to add a comparative example to Embedded Programming.
Coming from an embedded RTOS/ISR background, it was helpful to see the similarities in Qt's DirectConnection to Preemptive behavior of the ISRs and Qt's QueuedConnection to Queued Messages in an RTOS between tasks.
Side note: Coming from an Embedded background, it's difficult for me to not define the behavior in the programming. I never leave the argument as Auto, but that is just a personal opinion. I prefer everything to be explicitly written, and yes that gets difficult at times!

Can Qt call two slots simultaneously, if they get called from the same signal?

If there are two slots in two different threads and these slots are connected to a signal in a third thread. Can it happen, that both slots get called at the same time by the signal or do they get called synchronized every time?
I ask because i want to send some callback data structure (encapsulated with QSharedPointer) and ask if locking mechanism inside is needed.
You don't need to lock the actual signal/slot calls if you're using a Qt::QueuedConnection to pass the information to your threads, as the QueuedConnection mechanism handles this in a thread-safe manner.
That being said, you still need to protect any shared memory your threads access, regardless of how they were called. The fact that a third thread emitted a single signal to cause both slots to be called will not change this.
Have a look here (official Qt documentation for Qt's signal/slot mechanism regarding threads).
Each slot is called inside its thread, therefore I am pretty sure anything can happen. You should install a lock mechanism.