[Interest] QThread & sleep on QMutex

Keith Gardner kreios4004 at gmail.com
Thu Jul 30 14:34:49 CEST 2015


>
> And my question is just theoretical: is this approach good or such
> sleep() implementation not so good and why? I.e. will such sleep()
> release processor's time for another thread or not?
>

Using sleep() will allow the processor to work on a different thread. The
issue is that sleep() will most likely "over sleep" and make your code run
slower.

Also, under Windows, there is a 15ms penalty for blocking a thread (put to
sleep) by calling sleep() or using a mutex to lock. That means if you want
your thread to sleep for 5ms, it will sleep for at least 15ms. (This is
from my measurements on Windows XP and Windows 7 about five years ago. It
may be outdated.)

Best practices are to not to lock a thread for optimal performance. If you
need to block for synchronization purposes, it is best to use a condition
variable (QWaitCondition). This makes blocking a thread event driven by
using a different thread (the one you are waiting on) wake up the original
thread only at the proper time. In this way, your thread will never "over
sleep" but instead block for the minimum amount of time.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20150730/2f9449ae/attachment.html>


More information about the Interest mailing list