QWaitCondition 類為同步綫程提供條件變量。 更多...
| 頭: | #include <QWaitCondition> |
| qmake: | QT += core |
注意: 此類的所有函數 綫程安全 .
| 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() 本體像這樣:
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 ,和 等待條件範例 .
構造新等待條件對象。
銷毀等待條件對象。
此函數為兼容 STL (標準模闆庫) 提供。它相當於 wakeAll ().
該函數在 Qt 5.8 引入。
此函數為兼容 STL (標準模闆庫) 提供。它相當於 wakeOne ().
該函數在 Qt 5.8 引入。
釋放 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:
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.
該函數在 Qt 5.12 引入。
這是重載函數。
釋放 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:
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.
該函數在 Qt 5.12 引入。
這是重載函數。
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 ().
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 ().