Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Priority between zero timers and other sources of events



  • One one hand, documentation for QTimer says :

    [...]A QTimer with a timeout of 0 will time out as soon as possible, though the ordering between zero timers and other sources of events is unspecified. [...]

    One the other hand, documentation for QCoreApplication::exec says :

    [...]To make your application perform idle processing (by executing a special function whenever there are no pending events), use a QTimer with 0 timeout.[...]

    It seems to me there is a contradiction beetwen the two extracts (compare the parts I have highlighted)...


  • Lifetime Qt Champion

    I don't see a contradiction between those two sentences. Where exactly do you think there is a problem here?



  • @Christian-Ehrlicher

    Well, from the second sentence, I deduce that the handler connected to a zero timer will be triggered repeatedly as soon as possible, but never while there is still pending events to be processed in Qt's event loop.

    From the first sentence, I deduce that the handler connected to a zero timer will be triggered repeatedly as soon as possible, but with no guarantee that these timout events have a lower or a higher priority than the priorities of the pending events to be processed in Qt's event loop.


  • Lifetime Qt Champion

    QTimer with 0 msec is simply a signal with a queued connection so it is added to the event loop like any other events. When new events arrive after this event is added to the event loop they are executed after the timer event but I don't see any contradiction to the second sentence though.



  • This must be a wording issue...

    Let's start with this : does Qt's event loop implement a simple queue, a priority queue, or maybe something else ?
    Reading you, what you describes evokes a simple queue, but I'm not sure.


  • Lifetime Qt Champion

    Take a look into the sources. I don't see that this really matters in any way for normal use cases.



  • That's what I might do if nobody can answer me.
    Knowing what data structures are involved is essential in general.
    In this case it could be especially useful to compensate for the vagueness I am seeing in the documentation I quoted.

    For example, if Qt's event loop implements a simple queue, then what event should be picked from the queue to be processed is entirely determined by the one rule : FIFO. If so though, I really don't see what is meant by "the ordering between zero timers and other sources of events is unspecified" in the first sentence I've quoted.

    My case is quite normal I think. It just involves the use of a 0 timer. I want to understand what is said in the two sentences, which seem important.


  • Moderators

    @nostrapus said in Priority between zero timers and other sources of events:

    Let's start with this : does Qt's event loop implement a simple queue, a priority queue, or maybe something else ?

    I haven't looked at the implementation details, but it sounds like a priority queue: https://doc.qt.io/qt-5/qcoreapplication.html#postEvent



  • @JKSH
    Thanks for the link.

    A priority queue, that's what I thought, because of the word "ordering" in that context.

    In that light, my understanding of the second sentence I've quoted is that 0 timeouts have the lowest priority (to ensure they are triggered "whenever there are no pending events").

    I do see a contradiction with the first sentence I've quoted : I hardly see "the ordering between zero timers and other sources of events [being] unspecified" while the priority of zero timers is the lowest.


  • Moderators

    @nostrapus read it as
    the ordering between zero timers and other sources of events with the same priority is unspecified



  • @J-Hilk

    This way I get it and I agree !

    There remains a contradiction between your wording and what is said in the documentation pointed by @JKSH ("Events with equal priority will be processed in the order posted."), but I suppose the case of zero timer (your wording) can be seen as an exception to the general behavior of priority queues.

    Anyways, if your wording is the right, this is all fine for me and for my purpose. Thank you very much!


Log in to reply