[Development] QTimer question

Thiago Macieira thiago.macieira at intel.com
Tue May 30 01:14:40 CEST 2023


On Monday, 29 May 2023 11:40:14 PDT Jaroslaw Kobus via Development wrote:
> Hi All,
> 
> when I start 2 single shot timers synchronously in a row, with exactly the
> same interval, from the same thread, can I rely on having their handlers
> called in the same order in which they were started?
> 
> I.e.:
> 
> QTimer::singleShot(1000, [] { qDebug() << "1st timer elapsed"; });
> QTimer::singleShot(1000, [] { qDebug() << "2nd timer elapsed"; });
> 
> Should I always see the:
> 
> 1st timer elapsed
> 2nd timer elapsed
> 
> on the output, or should I expect the random order?

Strictly speaking, no, you can't. Because timers aren't precise, timers can be 
delivered early or late by 5% if they are Coarse. Therefore, the order will 
depend on which direction the rounding went, which depends on how long it took 
you between scheduling those two timers.

It will depend on the timeout too. For single-shot timers, timers below 2 
seconds are Precise, which means only those above that threshold will suffer 
reordering.

For non-QTimer::singleShot, all timers are Coarse by default.
-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Cloud Software Architect - Intel DCAI Cloud Engineering
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 5152 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/development/attachments/20230529/2357b6cd/attachment.bin>


More information about the Development mailing list