• PySide 模块
  • PySide.QtCore
  • 内容表

    上一话题

    QXmlStreamAttribute

    下一话题

    QSemaphore

    QWaitCondition

    概要

    函数

    详细描述

    PySide.QtCore.QWaitCondition class provides a condition variable for synchronizing threads.

    PySide.QtCore.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 PySide.QtCore.QWaitCondition to set a condition with PySide.QtCore.QWaitCondition.wakeOne() or PySide.QtCore.QWaitCondition.wakeAll() 。使用 PySide.QtCore.QWaitCondition.wakeOne() to wake one randomly selected condition or PySide.QtCore.QWaitCondition.wakeAll() to wake them all.

    例如,假设有 3 个任务应该履行每当用户按下键时。每个任务可以被分割成线程,每个线程都有 PySide.QtCore.QThread.run() 本体像这样:

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

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

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

    while True:
        getchar()
        keyPressed.wakeAll()
    										

    3 个线程被唤醒的次序不确定。另外,若某些线程仍在 do_something() 当键被按下时,它们不会被唤醒 (因为它们没有等待条件变量),所以任务不会因键按下而被履行。此问题可以被解决使用计数器和 PySide.QtCore.QMutex 去守卫它。例如,这里是工作者线程的新代码:

    while True:
        mutex.lock()
        keyPressed.wait(&mutex)
        count += 1
        mutex.unlock()
        do_something()
        mutex.lock()
        count -= 1
        mutex.unlock()
    									

    这里是第 4 个线程的代码:

    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 个线程的结果是不可预测的。

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

    class PySide.QtCore. QWaitCondition

    构造新等待条件对象。

    PySide.QtCore.QWaitCondition. wait ( readWriteLock [ , time=ULONG_MAX ] )
    参数:
    返回类型:

    PySide.QtCore.bool

    Releases the locked readWriteLock 并等待等待条件。 readWriteLock must be initially locked by the calling thread. If readWriteLock is not in a locked state, this function returns immediately. The readWriteLock must not be locked recursively, otherwise this function will not release the lock properly. The readWriteLock will be unlocked, and the calling thread will block until either of these conditions is met:

    readWriteLock 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.

    PySide.QtCore.QWaitCondition. wait ( mutex [ , time=ULONG_MAX ] )
    参数:
    返回类型:

    PySide.QtCore.bool

    Releases the locked mutex 并等待等待条件。 mutex must be initially locked by the calling thread. If mutex is not in a locked state, this function returns immediately. If mutex is a recursive mutex, this function returns immediately. The mutex will be unlocked, and the calling thread will block until either of these conditions is met:

    The mutex 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.

    PySide.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.

    PySide.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.