[Interest] Expected event execution order in this multi-thread application

Richard Weickelt richard at weickelt.de
Mon Sep 30 07:51:57 CEST 2019

>> what is the expected event execution order in the following scenario?
>> - 2 Threads running their event loop
>> - Thread T1 is handling an event E1
>> - Thread T2 sends an event E2 to T1 (queued)
>> - Thread T1 (still handling E1) emits an event E3 to itself (direct) after
>> E2 has already been enqueued.
> You did not describe a happens-before relationship between the sending of 
> events E2 and E3. So the order of either is unspecified.
>> - E3 is a very long-running event. To prevent events from starving, T1 calls
>> QCoreApplication::processEvents() periodically.
>> Observed behaviour:
>> - E3 gets immediately executed
>> - E2 is executed after E3/E1 have completed
> Because you didn't force a E2 to be queued before E3. So the order of the 
> events in the T1 queue may have been E1-E3-E2.

After debugging a bit, I come to realize that my above description is incorrect.

- Thread T1 is handling an event E1
- Thread T1 sends E3 to itself (queued connection)
- Thread T2 sends an event E2 to T1 (queued connection)
- Thread T1 handles E3 after completing E1.
- Thread T1 while handling E3 calls QCoreApplication::processEvents()
- E2 is sitting in the event queue of T1 at the second position but gets
never executed.

I think my question was misleading. I am not so much interested in the order
of arrival, but rather in "when" E2 gets executed by T1. I am fine with any
arrival order and start of execution as long as
QCoreApplication::processEvents() executes pending events while E3 is
running. But that is not the case in above scenario. E2 is such a pending
event, yet it doesn't get executed.

> The solution is to take a look at your threading code and see if you need a 
> synchronisation.

Sure, but I don't see a synchronization problem as long if
QCoreApplication::processEvents() would do what the documentation says:
executing pending events in the current thread. Am I misunderstanding something?


More information about the Interest mailing list