Question

Consider we have two threads: Thread1 and Thread2.

Thread1 has two signals: signal1() and signal2()

Thread2 has two slots: slot1() and slot2()

When we connect these signals and slot like:

connect(Thread1,SIGNAL(signal1()),Thread2,SLOT(slot1()));
connect(Thread1,SIGNAL(signal2()),Thread2,SLOT(slot2()));

And emit the signals one after another somewhere in Thread1 like:

emit signal1();
emit signal2();

As they are in different queued connections, does slot1() necessarily gets called before slot2() ?

Was it helpful?

Solution

The slots are executed in the emission order of their corresponding signals as of today.

What happens in the background is that the queued connection puts an event into the queue and that will get processed. This queue is basically a FIFO (first-in-first-out) as it currently is implemented.

Here you can find some details if you wish to check out the implementation yourself:

void QCoreApplication::postEvent(QObject *receiver, QEvent *event, int priority)

and

class QPostEventList : public QVector

However, I would personally suggest not to rely on this internal behavior as it is not documented and such, it can probably be subject for further changes without any notice.

Therefore, in my opinion, the more future proof way is to make explicit dependency management in your software if you need to rely on the other in some way. This is usually not a problem and even makes the code more explicit, thus more comprehensive.

OTHER TIPS

Your question is not well defined. Do you mean QThread objects by Thread1/Thread2?

In that case if the connection is Queued and the two objects have different thread affinity, then when a signal is emitted, it is wrapped in an event and placed in the event queue of the SLOT object (Thread2 in this case). These events are then processed (corresponding slot executed) sequentially, in the order they arrive. So yes, slot2() will be executed after slot1().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top