|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.trolltech.qt.internal.QSignalEmitterInternal
com.trolltech.qt.QSignalEmitter
com.trolltech.qt.QtJambiObject
com.trolltech.qt.core.QWaitCondition
public class QWaitCondition
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()
or wakeAll()
. Use wakeOne()
to wake one randomly selected condition or wakeAll()
to wake them all.
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() body like this:
while (true) { mutex.lock(); keyPressed.wait(mutex); do_something(); mutex.unlock(); }Here, the 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:
while (true) { 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: while (true) { mutex.lock(); keyPressed.wait(mutex); ++count; mutex.unlock(); do_something(); mutex.lock(); --count; mutex.unlock(); }Here's the code for the fourth thread:
while (true) { getchar(); mutex.lock(); // Sleep until there are no busy worker threads while (count > 0) { mutex.unlock(); try { Thread.sleep(1); } catch (InterruptedException e) { // handle exception } 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. The Wait Conditions example shows how to use QWaitCondition as an alternative to QSemaphore
for controlling access to a circular buffer shared by a producer thread and a consumer thread.
QMutex
, QSemaphore
, QThread, and Wait Conditions Example.
Nested Class Summary |
---|
Nested classes/interfaces inherited from class com.trolltech.qt.QSignalEmitter |
---|
QSignalEmitter.AbstractSignal, QSignalEmitter.Signal0, QSignalEmitter.Signal1, QSignalEmitter.Signal2, QSignalEmitter.Signal3, QSignalEmitter.Signal4, QSignalEmitter.Signal5, QSignalEmitter.Signal6, QSignalEmitter.Signal7, QSignalEmitter.Signal8, QSignalEmitter.Signal9 |
Nested classes/interfaces inherited from class com.trolltech.qt.internal.QSignalEmitterInternal |
---|
com.trolltech.qt.internal.QSignalEmitterInternal.AbstractSignalInternal |
Field Summary |
---|
Fields inherited from class com.trolltech.qt.internal.QSignalEmitterInternal |
---|
currentSender |
Constructor Summary | |
---|---|
QWaitCondition()
Constructs a new wait condition object. |
Method Summary | |
---|---|
boolean |
wait(QMutex mutex)
Releases the locked mutex and waits on the wait condition. |
boolean |
wait(QMutex mutex,
int time)
Releases the locked mutex and waits on the wait condition. |
boolean |
wait(QReadWriteLock readWriteLock)
Releases the locked readWriteLock and waits on the wait condition. |
boolean |
wait(QReadWriteLock readWriteLock,
int time)
Releases the locked readWriteLock and waits on the wait condition. |
void |
wakeAll()
Wakes all threads waiting on the wait condition. |
void |
wakeOne()
Wakes one thread waiting on the wait condition. |
Methods inherited from class com.trolltech.qt.QtJambiObject |
---|
dispose, disposed, equals, finalize, reassignNativeResources, tr, tr, tr |
Methods inherited from class com.trolltech.qt.QSignalEmitter |
---|
blockSignals, disconnect, disconnect, signalsBlocked, signalSender, thread |
Methods inherited from class com.trolltech.qt.internal.QSignalEmitterInternal |
---|
__qt_signalInitialization |
Methods inherited from class java.lang.Object |
---|
clone, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Methods inherited from interface com.trolltech.qt.QtJambiInterface |
---|
disableGarbageCollection, nativeId, nativePointer, reenableGarbageCollection, setJavaOwnership |
Constructor Detail |
---|
public QWaitCondition()
Method Detail |
---|
public final boolean wait(QMutex mutex)
wakeOne()
, and wakeAll()
.
public final boolean wait(QMutex mutex, int time)
wakeOne()
, and wakeAll()
.
public final boolean wait(QReadWriteLock readWriteLock)
wakeOne()
, and wakeAll()
.
public final boolean wait(QReadWriteLock readWriteLock, int time)
wakeOne()
, and wakeAll()
.
public final void wakeAll()
wakeOne()
.
public final void wakeOne()
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()
.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |