[Interest] Thread guarantee on slots+timers
André Somers
andre at familiesomers.nl
Fri Apr 11 10:26:15 CEST 2014
Sensei schreef op 11-4-2014 10:04:
> Dear all,
>
> quite a simple question here. I have an object living in a QThread, and
> this object has a timer executing a public slot every X seconds.
>
> If by chance I force calling the slot and meanwhile the timer emits a
> timeout() (or the other way round FWIW), am I guaranteed that the slot
> executes (by force) and next the slots gets run (by the timer) ?
>
> My guess is: yes, since I have only one event loop. However, asking for
> confirmation is better :)
>
Not quite, and it is not such a simple question at all. It depends on
which thread actually triggers the slot, and how. If you were to trigger
the slot, either by a direct method call or using a direct connection,
from two different threads, there are no guarantees of it being run
consequtively. It may run twice at the same time from two different
threads, or interleaved, or... There is just no telling.
If your slot is exclusively ran in a single thread, either because it is
only triggered from that thread or because you use queued connections
from other threads to trigger it, it will be ran consecutively for each
invocation. However, invocations that find their source in the event
queue (timers, but also queued connections) may be triggered later than
those that result from direct method calls or direct (or automatic,
within the same thread) connections. This is because the event queue is
only processed again if you return to it. Before that, timer events or
queued signals are not processed and thus don't trigger the slots.
André
More information about the Interest
mailing list