QWaitCondition Class Reference

[ QtCore module]

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

方法


详细描述

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 () 去唤醒它们所有。

For example, let's suppose that we have three tasks that should be performed whenever the user presses a key. Each task could be split into a thread, each of which would have a run() 本体像这样:

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

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

A fourth thread would read key presses and wake the other three threads up every time it receives one, like this:

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

The order in which the three threads are woken up is undefined. Also, if some of the threads are still in do_something() when the key is pressed, they won't be woken up (since they're not waiting on the condition variable) and so the task will not be performed for that key press. This issue can be solved using a counter and a QMutex to guard it. For example, here's the new code for the worker threads:

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

The mutex is necessary because the results of two threads attempting to change the value of the same variable simultaneously are unpredictable.

Wait conditions are a powerful thread synchronization primitive. Wait Conditions 范例展示如何使用 QWaitCondition 替代 QSemaphore for controlling access to a circular buffer shared by a producer thread and a consumer thread.


方法文档编制

QWaitCondition.__init__ ( self )

构造新等待条件对象。

bool QWaitCondition.wait ( self , QMutex   mutex , int  msecs  = ULONG_MAX)

Releases the locked mutex and waits on the wait condition. The 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.

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

bool QWaitCondition.wait ( self , QReadWriteLock   readWriteLock , int  msecs  = ULONG_MAX)

Releases the locked readWriteLock and waits on the wait condition. The 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.

该函数在 Qt 4.4 引入。

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

QWaitCondition.wakeAll ( self )

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

QWaitCondition.wakeOne ( self )

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