[Qt-interest] QWaitCondition prevents signals from calling slots

Thiago Macieira thiago.macieira at trolltech.com
Wed May 20 08:48:56 CEST 2009


Jonathan Wong wrote:
>Actually, further tests showed that a thread can both wait on a
> QWaitCondition and wait for incoming signals to its slots. But this
> thread must be a different one from the thread emitting the signals.
>
>1. ThreadA waits on a QWaitCondition.
>2. ThreadA also has a slot someSlot.
>3. Main thread starts a QTimer (to fire after ThreadA started waiting).
>4. QTimer successfully sends signal to someSlot.
>5. someSlot wakes on the QWaitCondition.
>6. ThreadA gets woken up from the wait in step 1.
>
>The doesn't happen if the same thread does all 3 things:
>
>1. Wait on a QWaitCondition.
>2. Has a slot someSlot that wakes up the wait.
>3. Has a QTimer that fires a signal to someSlot.

It's the same explanation as before.

Timers depend on the event loop running. And if the thread is blocked on a 
wait condition, then it's not running the event loop (the inverse is also 
true: if the event loop is running, then we're not waiting on a wait 
condition).

The difference between your first and second cases above is which thread has 
the timer. A timer is basically a way to ask the event loop to send you an 
event after timeout. So you can only use timers from threads that are 
running event loops. This restriction is exactly because of the problem 
you're seeing: if the thread isn't running an event loop, then the timer 
will never be processed.

One more thing: avoid having slots in the QThread-derived class itself. 
Remember that the QThread-derived object is created in another thread (it 
can't be created on its own thread because that thread isn't running 
yet!), so it has thread affinity to the creating thread. That may cause 
unexpected behaviour, so the recommendation is to avoid it.
-- 
Thiago Macieira - thiago.macieira (AT) nokia.com
  Senior Product Manager - Nokia, Qt Software
      Sandakerveien 116, NO-0402 Oslo, Norway
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090520/88609733/attachment.bin 


More information about the Qt-interest-old mailing list