<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[QWaitCondition::wait() and QMutex question]]></title><description><![CDATA[<p dir="auto">I have a question regarding QWaitCondition::wait() and the QMutex that it needs:</p>
<p dir="auto">According to the docs, QWaitCondition::wait() requires the calling thread to pass a pointer to a QMutex and that QMutex needs to be "locked" by the calling thread at the moment of the call. It's not said explicitly in the docs, but I assume that I can not use an <em>arbitrary</em> QMutex. Instead, I assume, <em>all</em> threads which want to wait() on the <em>same</em> QWaitCondition object also need to lock and pass the <em>same</em> "global" QMutex object. Right?</p>
<p dir="auto">Entering the wait() from different threads with the same QMutex is okay, because, according to the docs, wait() will <em>unlock</em> the QMutex when the "waiting" state is entered. So the first thread can lock the QMutex and enter the wait(). Then the second thread can lock the QMutex and also enter the wait(). Then the third thread. And so.</p>
<p dir="auto">But there is one thing that bothers me:</p>
<p dir="auto">According to the docs, the QMutex will be <em>locked</em> again when wait() returns! This might be okay, when QWaitCondition::wakeOne() is called. Only one thread will wake up and <em>this</em> thread will have the QMutex locked at the moment when wait() returns. But what about QWaitCondition::wakeAll()? If multiple threads were waiting, all these threads will wake up <em>at the same time</em>, when wakeAll() gets called! It's not possible that multiple threads have the QMutex locked at the same time. So which of these threads will have the QMutex locked? Is it undefined?</p>
<p dir="auto">But then how do I know which of threads that were waiting now needs to unlock the "global" QMutex?</p>
]]></description><link>https://forum.qt.io/topic/16478/qwaitcondition-wait-and-qmutex-question</link><generator>RSS for Node</generator><lastBuildDate>Sat, 25 Apr 2026 05:02:50 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/16478.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 05 May 2012 00:47:19 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QWaitCondition::wait() and QMutex question on Thu, 10 May 2012 06:54:14 GMT]]></title><description><![CDATA[<p dir="auto">Good question, I don't know if each call to <em>wait()</em> on the same wait condition can use a different mutex. If it's not possible you could always resort to using multiple wait conditions, one for each thread that blocks, and put them in a list. Then when you want them all to wake up you call <em>wakeOne()</em> on each wait condition in the list.</p>
]]></description><link>https://forum.qt.io/post/138526</link><guid isPermaLink="true">https://forum.qt.io/post/138526</guid><dc:creator><![CDATA[capisce]]></dc:creator><pubDate>Thu, 10 May 2012 06:54:14 GMT</pubDate></item><item><title><![CDATA[Reply to QWaitCondition::wait() and QMutex question on Wed, 09 May 2012 23:24:22 GMT]]></title><description><![CDATA[<p dir="auto">Thanks for the info. But there is one thing that still isn't clear to me and I think the docs aren't precise on this: If I don't need the additional Mutex locking in my application (I just want several threads to be "blocked" until another threads triggers the wakeup and I want them all to wake up as soon as possible when that happens), can each thread use its own QMutex instance to call <em>QWaitCondition::wait()</em> or is it a <em>requirement</em> that all threads, which want to wait() on the same <em>QWaitCondition</em> object, need to enter wait() using the exactly same QMutex?</p>
]]></description><link>https://forum.qt.io/post/138518</link><guid isPermaLink="true">https://forum.qt.io/post/138518</guid><dc:creator><![CDATA[MuldeR]]></dc:creator><pubDate>Wed, 09 May 2012 23:24:22 GMT</pubDate></item><item><title><![CDATA[Reply to QWaitCondition::wait() and QMutex question on Wed, 09 May 2012 06:22:11 GMT]]></title><description><![CDATA[<p dir="auto">If there is another <em>QWaitCondition::wakeAll()</em> call that will only affect the threads that have already been woken up by the first call and then gone back to sleep by calling <em>wait()</em> again.  The other threads would have woken up in any case, so that's not changed by the new call to <em>QWaitCondition::wakeAll()</em>. There's no per-thread counter in the wait condition saying how many times that thread should be woken up, it's just about waking up the threads that are currently in <em>wait()</em> on the same wait condition.</p>
<p dir="auto">See <a href="http://qt-project.org/doc/qt-4.8/qwaitcondition.html#details" target="_blank" rel="noopener noreferrer nofollow ugc">http://qt-project.org/doc/qt-4.8/qwaitcondition.html#details</a></p>
]]></description><link>https://forum.qt.io/post/138394</link><guid isPermaLink="true">https://forum.qt.io/post/138394</guid><dc:creator><![CDATA[capisce]]></dc:creator><pubDate>Wed, 09 May 2012 06:22:11 GMT</pubDate></item><item><title><![CDATA[Reply to QWaitCondition::wait() and QMutex question on Tue, 08 May 2012 21:30:54 GMT]]></title><description><![CDATA[<p dir="auto">So do I get this right? <em>QWaitCondition::wakeAll()</em> doesn't wake all waiting threads right away, but instead will only wake a single thread (chosen arbitrarily) and give <em>that</em> thread ownership of the QMutex. As soon as the first thread that was woken up releases the QMutex, the next thread will be woken up. And so on...</p>
<p dir="auto">If so, what happens if (a) there is another <em>QWaitCondition::wakeAll()</em> call while the previous wakeAll() is still in the process of waking up the threads or if (b) new threads start to wait for the QWaitCondition while a <em>QWaitCondition::wakeAll()</em> is still process of waking up the threads ???</p>
]]></description><link>https://forum.qt.io/post/138386</link><guid isPermaLink="true">https://forum.qt.io/post/138386</guid><dc:creator><![CDATA[MuldeR]]></dc:creator><pubDate>Tue, 08 May 2012 21:30:54 GMT</pubDate></item><item><title><![CDATA[Reply to QWaitCondition::wait() and QMutex question on Tue, 08 May 2012 20:05:40 GMT]]></title><description><![CDATA[<p dir="auto">As far as I know the thread is chosen arbitrarily. Each thread is woken, so once the first thread that is woken releases its lock the next thread continues execution, etc.</p>
]]></description><link>https://forum.qt.io/post/138380</link><guid isPermaLink="true">https://forum.qt.io/post/138380</guid><dc:creator><![CDATA[capisce]]></dc:creator><pubDate>Tue, 08 May 2012 20:05:40 GMT</pubDate></item></channel></rss>