[Qt-interest] QWaitCondition prevents signals from calling slots

Jonathan Wong jhannwong at gmail.com
Wed May 20 05:51:31 CEST 2009


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.

I couldn't find any online docs explaining the relations between threads and signals. Couldn't
find the word "thread" here: http://doc.trolltech.com/4.5/signalsandslots.html

If anyone is interested to explore this further, I've included some sample codes. It has a
convenient output to GUI (QTextEdit), so you can check the different permutations I mentioned above.

Let me know if there's any need to write docs on this, or if proper Qt usage renders moot any
consideration of this (threads and signals thing). Somehow, I suspect there might be a need to
wait on QWaitCondition and listen for incoming signals too.

One of my colleagues spent many hours confused by this. He did a loop, I "corrected" it with a
QWaitCondition (akin to sleep), but found that slots are not called in either case. Yes, I sent us
on the correct event-driven model after that.

Thanks for your help in this!


Thiago Macieira wrote:
> Jonathan Wong wrote:
>> I do understand that doing a loop or QWaitCondition wait defeats the
>> event-driven paradigm. Feel free to repeat this answer to me to confirm
>> my understanding.
>>
>> Jonathan Wong wrote:
>>> Seems that we should not wait on a QWaitCondition if we want our slots
>>> to be alive and responding to signals.
>>>
>>> Is that true?
> 
> It's true and your explanation above is also correct :-)
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest

-------------- next part --------------
A non-text attachment was scrubbed...
Name: QWaitSignalsTest.7z
Type: application/octet-stream
Size: 1971 bytes
Desc: not available
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090520/9df8d7c8/attachment.obj 


More information about the Qt-interest-old mailing list