Qt-interest Archive, June 2007
A clean way to sleep a thread?
Message 1 in thread
Hello all,
i've got a QObject-based class instantiated in a QThread run(), name
it Timer. It just encapsulates a QTimer and some support logic. Now
from inside the Timer, i want to suspend the thread for some defined
time, just like Sleep() of Win32 fame - actually i am porting some
MFC-heavy code to Qt. I don't care about stopped event loop, it's
really simple dedicated thread. What is the nice and correct way to do
it in Qt? QThread::sleep (msleep, usleep) is protected.
QThread::wait() is not intended for this usage, as it displays "Thread
tried to wait on itself". To solve things fast, I have eventually
ended with a public method of my QThread-derived class, which only
calls QThread::msleep, and i am casting thread() inside Timer to be
able to call it. Is that as dirty as i feel about it?
Thank you,
Pavel Zdenek
--
[ signature omitted ]
Message 2 in thread
On Wednesday 06 June 2007 11:57:44 Pavel Zdenek wrote:
> To solve things fast, I have eventually ended with a public method of my
> QThread-derived class, which only calls QThread::msleep, and i am casting
> thread() inside Timer to be able to call it. Is that as dirty as i feel
> about it?
Yes, it is :) This is the way that I suggest doing it to anyone that asks this
question, too. Sleeping in a thread is usually an indication of lacking
synchronization between threads.
--
[ signature omitted ]
Message 3 in thread
2007/6/6, Bradley T Hughes <bhughes@xxxxxxxxxxxxx>:
> On Wednesday 06 June 2007 11:57:44 Pavel Zdenek wrote:
> > To solve things fast, I have eventually ended with a public method of my
> > QThread-derived class, which only calls QThread::msleep, and i am casting
> > thread() inside Timer to be able to call it. Is that as dirty as i feel
> > about it?
>
> Yes, it is :) This is the way that I suggest doing it to anyone that asks this
> question, too. Sleeping in a thread is usually an indication of lacking
> synchronization between threads.
>
Ok, in case of internal syncing of my application, i would certainly
not do it this way. More technically, i am querying an external hw
device and doing that 3x to be sure. The device is of unknown,
possibly very slow access speed, hence a thread. I am supposed to wait
couple of millis between the queries. The purpose of the encapsulated
QTimer is to launch this querying each couple of minutes. Given the
fact that the thread has nothing else to do, having an observer or a
wait condition driven by extra timer or whatever, seems like an
overkill to me and well over the scope of "just porting some MFC". So
thanks for the satisfaction that i am not doing something inherently
bad, just a little misbehavior driven by laziness. Which you guys
surely targeted with making QThread::sleep protected :)
--
[ signature omitted ]