QWaitCondition 類

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

頭: #include <QWaitCondition>
qmake: QT += core

注意: 此類的所有函數 綫程安全 .

公共函數

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

詳細描述

The QWaitCondition class provides a condition variable for synchronizing threads.

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 () 或 wakeAll ()。使用 wakeOne () 去喚醒某一隨機選中綫程或 wakeAll () 去喚醒它們所有。

例如,假設有 3 個任務應該履行每當用戶按下鍵時。每個任務可以被分割成綫程,每個綫程都有 run() 本體像這樣:

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

在這裏, keyPressed variable is a global variable of type 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 as an alternative to QSemaphore 為控製由生産者綫程和消費者綫程共享的循環緩衝的訪問。

另請參閱 QMutex , QSemaphore , QThread ,和 等待條件範例 .

成員函數文檔編製

QWaitCondition:: QWaitCondition ()

構造新等待條件對象。

QWaitCondition:: ~QWaitCondition ()

銷毀等待條件對象。

void QWaitCondition:: notify_all ()

此函數為兼容 STL (標準模闆庫) 提供。它相當於 wakeAll ().

該函數在 Qt 5.8 引入。

void QWaitCondition:: notify_one ()

此函數為兼容 STL (標準模闆庫) 提供。它相當於 wakeOne ().

該函數在 Qt 5.8 引入。

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

釋放 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.
  • time 毫秒已過去。若 time is ULONG_MAX (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 ( QReadWriteLock * lockedReadWriteLock , unsigned long time = ULONG_MAX)

釋放 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.
  • time 毫秒已過去。若 time is ULONG_MAX (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.

該函數在 Qt 4.4 引入。

另請參閱 wakeOne () 和 wakeAll ().

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