内容表

上一话题

QVersionNumber

下一话题

QWriteLocker

QWaitCondition

QWaitCondition class provides a condition variable for synchronizing threads. 更多

Inheritance diagram of PySide2.QtCore.QWaitCondition

概要

函数

详细描述

QWaitCondition allows a thread to tell other threads that some sort of condition has been met. One or many threads can block waiting for a QWaitCondition to set a condition with wakeOne() or wakeAll() 。使用 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() 本体像这样:

while True:
    mutex.lock()
    keyPressed.wait(mutex)
    do_something()
    mutex.unlock()
											

在这里, keyPressed variable is a global variable of type QWaitCondition .

第 4 个线程将读取键按下,并在每次收到一个时唤醒其他 3 个线程,像这样:

while True:
    getchar()
    keyPressed.wakeAll()
											

3 个线程被唤醒的次序不确定。另外,若某些线程仍在 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 QMutex 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()
											

互斥是必要的,因为试图同时改变同一变量值的 2 个线程的结果是不可预测的。

等待条件是强大的线程同步原语。 等待条件范例 example shows how to use QWaitCondition as an alternative to QSemaphore 为控制由生产者线程和消费者线程共享的循环缓冲的访问。

class QWaitCondition

构造新等待条件对象。

PySide2.QtCore.QWaitCondition. notify_all ( )

此函数是为兼容 STL 而提供的。它相当于 wakeAll() .

PySide2.QtCore.QWaitCondition. notify_one ( )

此函数是为兼容 STL 而提供的。它相当于 wakeOne() .

PySide2.QtCore.QWaitCondition. wait ( lockedMutex [ , deadline=QDeadlineTimer(QDeadlineTimer.Forever) ] )
参数
返回类型

bool

释放 lockedMutex 并等待等待条件。 lockedMutex must be initially locked by the calling thread. If lockedMutex is not in a locked state, the behavior is undefined. If lockedMutex is a recursive mutex, this function returns immediately. The lockedMutex will 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 deadline is reached. If deadline is 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.

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 )
参数
返回类型

bool

这是重载函数。

PySide2.QtCore.QWaitCondition. wait ( lockedReadWriteLock [ , deadline=QDeadlineTimer(QDeadlineTimer.Forever) ] )
参数
返回类型

bool

释放 lockedReadWriteLock 并等待等待条件。 lockedReadWriteLock must be initially locked by the calling thread. If lockedReadWriteLock is not in a locked state, this function returns immediately. The lockedReadWriteLock must not be locked recursively, otherwise this function will not release the lock properly. The lockedReadWriteLock will 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 deadline is reached. If deadline is 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.

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 )
参数
返回类型

bool

这是重载函数。

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.

另请参阅

wakeOne()

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.

另请参阅

wakeAll()