PySide6.QtCore.QWaitCondition¶
- class QWaitCondition¶
- The - QWaitConditionclass provides a condition variable for synchronizing threads. More…- Synopsis¶- Methods¶- def - __init__()
- def - notify_all()
- def - notify_one()
- def - wait()
- def - wakeAll()
- def - wakeOne()
 - Note - This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE - Detailed Description¶- Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - QWaitConditionallows a thread to tell other threads that some sort of condition has been met. One or many threads can block waiting for a- QWaitConditionto set a condition with- wakeOne()or- wakeAll(). Use- wakeOne()to wake one randomly selected thread or- wakeAll()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:- forever { mutex.lock() keyPressed.wait(mutex) do_something() mutex.unlock() - Here, the - keyPressedvariable is a global variable of type- QWaitCondition.- A fourth thread would read key presses and wake the other three threads up every time it receives one, like this: - forever { 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 a- QMutexto guard it. For example, here’s the new code for the worker threads:- forever { 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: - forever { 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 Producer and Consumer using Wait Conditions example shows how to use - QWaitConditionas an alternative to- QSemaphorefor controlling access to a circular buffer shared by a producer thread and a consumer thread.- See also - QMutex- QSemaphore- QThreadProducer and Consumer using Wait Conditions- __init__()¶
 - Constructs a new wait condition object. - notify_all()¶
 - This function is provided for STL compatibility. It is equivalent to - wakeAll().- notify_one()¶
 - This function is provided for STL compatibility. It is equivalent to - wakeOne().- wait(lockedMutex[, deadline=QDeadlineTimer(QDeadlineTimer.Forever)])¶
- Parameters:
- lockedMutex – - QMutex
- deadline – - QDeadlineTimer
 
- Return type:
- bool 
 
 - Releases the - lockedMutexand waits on the wait condition. The- lockedMutexmust be initially locked by the calling thread. If- lockedMutexis not in a locked state, the behavior is undefined. If- lockedMutexis a recursive mutex, this function returns immediately. The- lockedMutexwill be unlocked, and the calling thread will block until either of these conditions is met:- Another thread signals it using - wakeOne()or- wakeAll(). This function will return true in this case.
- the deadline given by - deadlineis reached. If- deadlineis- QDeadlineTimer::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 - lockedMutexwill be returned to the same locked state. This function is provided to allow the atomic transition from the locked state to the wait state.- wait(lockedMutex, time)
- Parameters:
- lockedMutex – - QMutex
- time – int 
 
- Return type:
- bool 
 
 - This is an overloaded function. - Releases the - lockedMutexand waits on the wait condition for- timemilliseconds.- wait(lockedReadWriteLock[, deadline=QDeadlineTimer(QDeadlineTimer.Forever)])
- Parameters:
- lockedReadWriteLock – - QReadWriteLock
- deadline – - QDeadlineTimer
 
- Return type:
- bool 
 
 - Releases the - lockedReadWriteLockand waits on the wait condition. The- lockedReadWriteLockmust be initially locked by the calling thread. If- lockedReadWriteLockis not in a locked state, this function returns immediately. The- lockedReadWriteLockmust not be locked recursively, otherwise this function will not release the lock properly. The- lockedReadWriteLockwill be unlocked, and the calling thread will block until either of these conditions is met:- Another thread signals it using - wakeOne()or- wakeAll(). This function will return true in this case.
- the deadline given by - deadlineis reached. If- deadlineis- QDeadlineTimer::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 - lockedReadWriteLockwill be returned to the same locked state. This function is provided to allow the atomic transition from the locked state to the wait state.- wait(lockedReadWriteLock, time)
- Parameters:
- lockedReadWriteLock – - QReadWriteLock
- time – int 
 
- Return type:
- bool 
 
 - This is an overloaded function. - Releases the - lockedReadWriteLockand waits on the wait condition for- timemilliseconds.- 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 - 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