QWaitCondition 類

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

頭: #include <QWaitCondition>

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

公共函數

QWaitCondition ()
~QWaitCondition ()
bool wait (QMutex * mutex , unsigned long time = ULONG_MAX)
bool wait (QReadWriteLock * readWriteLock , 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 () to wake one randomly selected condition or 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 個綫程的結果是不可預測的。

等待條件是強大的綫程同步原語。 Wait Conditions 範例展示如何使用 QWaitCondition as an alternative to QSemaphore 為控製由生産者綫程和消費者綫程共享的循環緩衝的訪問。

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

成員函數文檔編製

QWaitCondition:: QWaitCondition ()

構造新等待條件對象。

QWaitCondition:: ~QWaitCondition ()

銷毀等待條件對象。

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

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:

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

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

bool QWaitCondition:: wait ( QReadWriteLock * readWriteLock , unsigned long time = ULONG_MAX)

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:

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

該函數在 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 ().