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

Thiago Macieira thiago.macieira at intel.com
Sun Sep 29 18:36:37 CEST 2019


On Sunday, 29 September 2019 02:49:36 PDT Richard Weickelt wrote:
> Hello,
> 
> 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.

> Is this behavior expected? I would expected E2 being executed at least on
> the first invocation of QCoreApplication::processEvents(), but apparently it
> sits in the event queue until T1 returns from E3 (and also E1).

It's expected from your description. It's not the only possibility, though, 
since the order is unspecified.

> In my scenario E2 is rather short and E3 expectes something from E2 in order
> to complete. So I run into a deadlock here.
> 
> Would it be a solution to handle E3 in a queued connection? In case E2 is
> posted after E3 has started, would E2 be executed by processEvents()?

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

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel System Software Products





More information about the Interest mailing list