In Qt multiple threads use 'emit' to send a large number of signals, the trigger is very slow. Seemingly there is a queuing mechanism. Is there any good way to quickly trigger a signal slot on the line?
Asked
Active
Viewed 1,070 times
1
-
Do you want the slot to be called in the thread that emits the signal? If so, make sure it is thread safe, and everything it calls is also thread safe! In particular, GUI stuff must all happen in the main thread! – hyde Oct 27 '16 at 05:05
1 Answers
2
What you are asking about is called Qt::DirectConnection
. You need to specify it in connect()
to guarantee slot will be invoked immediately.
When it is used:
The slot is invoked immediately when the signal is emitted. The slot is executed in the signalling thread.
Otherwise, the default Qt way is called Qt::AutoConnection
.
If the receiver lives in the thread that emits the signal, Qt::DirectConnection is used. Otherwise, Qt::QueuedConnection is used. The connection type is determined when the signal is emitted.
You can specify connection type in connect()
:
QMetaObject::Connection QObject::connect(const QObject *sender, const
char *signal, const QObject *receiver, const char *method,
Qt::ConnectionType type = Qt::AutoConnection)
Very good answer about the difference between these two and how to use is here.
-
Of course then the slot method must be thread safe, which excludes updaring the GUI, for example. It might be better to separate the logic into two related classes, one having the thread safe slots and another the "normal" slots, to avoid accidentally doing direct inter-thread connection for thread-unsafe slot. – hyde Oct 27 '16 at 05:11
-
@hyde Agree but we don't know what topic starter wants to do in slots. He asks just how to call slots without queue :) – demonplus Oct 27 '16 at 05:14
-
1Nitpicking, but I wouldn't say *"Qt::AutoConnection which in most cases is the same as Qt::QueuedConnection"*, because it is queued between threads and direct in same thread always, there's no "most cases". – hyde Oct 27 '16 at 06:04
-
@hyde ok, added some perfect info from docs, instead of most cases. Hope you will like it now – demonplus Oct 27 '16 at 06:11