QWaitCondition 类

QWaitCondition 类为同步线程提供条件变量。 更多...

头: #include <QWaitCondition>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

注意: 此类的所有函数 thread-safe .

公共函数

QWaitCondition ()
~QWaitCondition ()
void notify_all ()
void notify_one ()
bool wait (QMutex * lockedMutex , QDeadlineTimer deadline = QDeadlineTimer(QDeadlineTimer::Forever))
bool wait (QMutex * lockedMutex , unsigned long time )
bool wait (QReadWriteLock * lockedReadWriteLock , QDeadlineTimer deadline = QDeadlineTimer(QDeadlineTimer::Forever))
bool wait (QReadWriteLock * lockedReadWriteLock , unsigned long time )
void wakeAll ()
void wakeOne ()

详细描述

QWaitCondition 允许线程告诉其它线程某种条件已被满足。可以阻塞一个或多个线程等待 QWaitCondition 设置条件采用 wakeOne () 或 wakeAll ()。使用 wakeOne () 去唤醒某一随机选中线程或 wakeAll () 去唤醒它们所有。

例如,假设有 3 个任务应该履行每当用户按下键时。每个任务可以被分割成线程,每个线程都有 run () body like this:

forever {
    mutex.lock();
    keyPressed.wait(&mutex);
    do_something();
    mutex.unlock();
}
					

在这里, keyPressed 变量是 QWaitCondition 类型的全局变量。

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

forever {
    getchar();
    keyPressed.wakeAll();
}
					

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

forever {
    mutex.lock();
    keyPressed.wait(&mutex);
    ++count;
    mutex.unlock();
    do_something();
    mutex.lock();
    --count;
    mutex.unlock();
}
					

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

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();
}
					

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

等待条件是强大的线程同步原语。 使用等待条件的生产者和消费者 范例展示如何使用 QWaitCondition 替代 QSemaphore 为控制由生产者线程和消费者线程共享的循环缓冲的访问。

另请参阅 QMutex , QSemaphore , QThread ,和 使用等待条件的生产者和消费者 .

成员函数文档编制

QWaitCondition:: QWaitCondition ()

构造新等待条件对象。

[noexcept] QWaitCondition:: ~QWaitCondition ()

销毁等待条件对象。

void QWaitCondition:: notify_all ()

此函数为兼容 STL (标准模板库) 提供。它相当于 wakeAll ().

void QWaitCondition:: notify_one ()

此函数为兼容 STL (标准模板库) 提供。它相当于 wakeOne ().

bool QWaitCondition:: wait ( QMutex * lockedMutex , QDeadlineTimer deadline = QDeadlineTimer(QDeadlineTimer::Forever))

释放 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 () 或 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.

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.

另请参阅 wakeOne () 和 wakeAll ().

bool QWaitCondition:: wait ( QMutex * lockedMutex , unsigned long time )

这是重载函数。

释放 lockedMutex and waits on the wait condition for time 毫秒。

bool QWaitCondition:: wait ( QReadWriteLock * lockedReadWriteLock , QDeadlineTimer deadline = QDeadlineTimer(QDeadlineTimer::Forever))

释放 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 () 或 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.

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.

另请参阅 wakeOne () 和 wakeAll ().

bool QWaitCondition:: wait ( QReadWriteLock * lockedReadWriteLock , unsigned long time )

这是重载函数。

释放 lockedReadWriteLock and waits on the wait condition for time 毫秒。

void 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 ().

void 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 ().