QWaitCondition¶
The
QWaitCondition
class provides a condition variable for synchronizing threads. More…
Synopsis¶
Functions¶
def
notify_all
()def
notify_one
()def
wait
(lockedMutex, time)def
wait
(lockedMutex[, deadline=QDeadlineTimer(QDeadlineTimer.Forever)])def
wait
(lockedReadWriteLock, time)def
wait
(lockedReadWriteLock[, deadline=QDeadlineTimer(QDeadlineTimer.Forever)])def
wakeAll
()def
wakeOne
()
Detailed Description¶
QWaitCondition
allows a thread to tell other threads that some sort of condition has been met. One or many threads can block waiting for aQWaitCondition
to set a condition withwakeOne()
orwakeAll()
. UsewakeOne()
to wake one randomly selected thread orwakeAll()
to wake them all.For example, let’s suppose that we have three tasks that should be performed whenever the user presses a key. Each task could be split into a thread, each of which would have a
run()
body like this:while True: mutex.lock() keyPressed.wait(mutex) do_something() mutex.unlock()Here, the
keyPressed
variable is a global variable of typeQWaitCondition
.A fourth thread would read key presses and wake the other three threads up every time it receives one, like this:
while True: getchar() keyPressed.wakeAll()The order in which the three threads are woken up is undefined. Also, if some of the threads are still in
do_something()
when the key is pressed, they won’t be woken up (since they’re not waiting on the condition variable) and so the task will not be performed for that key press. This issue can be solved using a counter and aQMutex
to guard it. For example, here’s the new code for the worker threads:while True: mutex.lock() keyPressed.wait(&mutex) count += 1 mutex.unlock() do_something() mutex.lock() count -= 1 mutex.unlock()Here’s the code for the fourth thread:
while True: getchar() mutex.lock() # Sleep until there are no busy worker threads while count > 0: mutex.unlock() sleep(1) mutex.lock() keyPressed.wakeAll() mutex.unlock()The mutex is necessary because the results of two threads attempting to change the value of the same variable simultaneously are unpredictable.
Wait conditions are a powerful thread synchronization primitive. The Wait Conditions Example example shows how to use
QWaitCondition
as an alternative toQSemaphore
for controlling access to a circular buffer shared by a producer thread and a consumer thread.See also
- class PySide2.QtCore.QWaitCondition¶
Constructs a new wait condition object.
- PySide2.QtCore.QWaitCondition.notify_all()¶
This function is provided for STL compatibility. It is equivalent to
wakeAll()
.
- PySide2.QtCore.QWaitCondition.notify_one()¶
This function is provided for STL compatibility. It is equivalent to
wakeOne()
.
- PySide2.QtCore.QWaitCondition.wait(lockedMutex[, deadline=QDeadlineTimer(QDeadlineTimer.Forever)])¶
- Parameters:
lockedMutex –
PySide2.QtCore.QMutex
deadline –
PySide2.QtCore.QDeadlineTimer
- Return type:
bool
Releases the
lockedMutex
and waits on the wait condition. ThelockedMutex
must be initially locked by the calling thread. IflockedMutex
is not in a locked state, the behavior is undefined. IflockedMutex
is a recursive mutex, this function returns immediately. ThelockedMutex
will be unlocked, and the calling thread will block until either of these conditions is met:Another thread signals it using
wakeOne()
orwakeAll()
. This function will return true in this case.the deadline given by
deadline
is reached. Ifdeadline
isQDeadlineTimer::Forever
(the default), then the wait will never timeout (the event must be signalled). This function will return false if the wait timed out.
The
lockedMutex
will be returned to the same locked state. This function is provided to allow the atomic transition from the locked state to the wait state.
- PySide2.QtCore.QWaitCondition.wait(lockedMutex, time)
- Parameters:
lockedMutex –
PySide2.QtCore.QMutex
time – int
- Return type:
bool
This is an overloaded function.
- PySide2.QtCore.QWaitCondition.wait(lockedReadWriteLock[, deadline=QDeadlineTimer(QDeadlineTimer.Forever)])
- Parameters:
lockedReadWriteLock –
PySide2.QtCore.QReadWriteLock
deadline –
PySide2.QtCore.QDeadlineTimer
- Return type:
bool
Releases the
lockedReadWriteLock
and waits on the wait condition. ThelockedReadWriteLock
must be initially locked by the calling thread. IflockedReadWriteLock
is not in a locked state, this function returns immediately. ThelockedReadWriteLock
must not be locked recursively, otherwise this function will not release the lock properly. ThelockedReadWriteLock
will be unlocked, and the calling thread will block until either of these conditions is met:Another thread signals it using
wakeOne()
orwakeAll()
. This function will return true in this case.the deadline given by
deadline
is reached. Ifdeadline
isQDeadlineTimer::Forever
(the default), then the wait will never timeout (the event must be signalled). This function will return false if the wait timed out.
The
lockedReadWriteLock
will be returned to the same locked state. This function is provided to allow the atomic transition from the locked state to the wait state.
- PySide2.QtCore.QWaitCondition.wait(lockedReadWriteLock, time)
- Parameters:
lockedReadWriteLock –
PySide2.QtCore.QReadWriteLock
time – int
- Return type:
bool
This is an overloaded function.
- PySide2.QtCore.QWaitCondition.wakeAll()¶
Wakes all threads waiting on the wait condition. The order in which the threads are woken up depends on the operating system’s scheduling policies and cannot be controlled or predicted.
See also
- PySide2.QtCore.QWaitCondition.wakeOne()¶
Wakes one thread waiting on the wait condition. The thread that is woken up depends on the operating system’s scheduling policies, and cannot be controlled or predicted.
If you want to wake up a specific thread, the solution is typically to use different wait conditions and have different threads wait on different conditions.
See also
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.