1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Qt Toolkit - QWaitCondition Class</title><style type="text/css"><!--
h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }body { background: white; color: black; }
--></style>
</head><body bgcolor="#ffffff">
<table width="100%">
<tr><td><a href="index.html">
<img width="100" height="100" src="qtlogo.png"
alt="Home" border="0"><img width="100"
height="100" src="face.png" alt="Home" border="0">
</a><td valign=top><div align=right><img src="dochead.png" width="472" height="27"><br>
<a href="classes.html"><b>Classes</b></a>
-<a href="annotated.html">Annotated</a>
- <a href="hierarchy.html">Tree</a>
-<a href="functions.html">Functions</a>
-<a href="index.html">Home</a>
-<a href="topicals.html"><b>Structure</b></a>
</div>
</table>
<h1 align=center>QWaitCondition Class Reference</h1><br clear="all">
<p>
The QWaitCondition class allows waiting/waking for conditions between threads
<a href="#details">More...</a>
<p>
<code>#include <<a href="qthread-h.html">qthread.h</a>></code>
<p>
Inherits <a href="qt.html">Qt</a>.
<p><a href="qwaitcondition-members.html">List of all member functions.</a>
<h2>Public Members</h2>
<ul>
<li><div class="fn"><a href="#eee0a1"><b>QWaitCondition</b></a>()</div>
<li><div class="fn">virtual<a href="#ee5dc6"><b>~QWaitCondition</b></a>()</div>
<li><div class="fn">bool<a href="#d24199"><b>wait</b></a>(unsignedlongtime=ULONG_MAX)</div>
<li><div class="fn">bool<a href="#e72dde"><b>wait</b></a>(QMutex*mutex, unsignedlongtime=ULONG_MAX)</div>
<li><div class="fn">void<a href="#57c32e"><b>wakeOne</b></a>()</div>
<li><div class="fn">void<a href="#3b1793"><b>wakeAll</b></a>()</div>
</ul>
<hr><h2><a name="details"></a>Detailed Description</h2>
The QWaitCondition class allows waiting/waking for conditions between threads
<p>
QWaitConditions allow 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 <a href="#57c32e">wakeOne</a>() or wakeAll. Use
wakeOne() to wake one randomly-selected event or <a href="#3b1793">wakeAll</a>() to wake them
all. For example, say we have three tasks that should be performed every
time the user presses a key; each task could be split into a thread, each
of which would have a run() body like so:
<p><pre> <a href="qwaitcondition.html">QWaitCondition</a> key_pressed;
for (;;) {
key_pressed.<a href="#d24199">wait</a>(); // This is a QWaitCondition global variable
// Key was pressed, do something interesting
do_something();
}
</pre>
<p>A fourth thread would read key presses and wake the other three threads
up every time it receives one, like so:
<p><pre> <a href="qwaitcondition.html">QWaitCondition</a> key_pressed;
for (;;) {
getchar();
// Causes any thread in key_pressed.<a href="#d24199">wait</a>() to return from
// that method and continue processing
key_pressed.<a href="#3b1793">wakeAll</a>();
}
</pre>
<p>Note that the order the three threads are woken up in is undefined,
and that if some or all 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 can be avoided by, for example, doing something
like this:
<p><pre> <a href="qmutex.html">QMutex</a> mymutex;
<a href="qwaitcondition.html">QWaitCondition</a> key_pressed;
int mycount=0;
// Worker thread code
for (;;) {
key_pressed.<a href="#d24199">wait</a>(); // This is a QWaitCondition global variable
mymutex.<a href="qmutex.html#4e6e5d">lock</a>();
mycount++;
mymutex.<a href="qmutex.html#da15c3">unlock</a>();
do_something();
mymutex.<a href="qmutex.html#4e6e5d">lock</a>();
mycount--;
mymutex.<a href="qmutex.html#da15c3">unlock</a>();
}
// Key reading thread code
for (;;) {
getchar();
mymutex.<a href="qmutex.html#4e6e5d">lock</a>();
// Sleep until there are no busy worker threads
while(count>0) {
mymutex.<a href="qmutex.html#da15c3">unlock</a>();
sleep(1);
mymutex.<a href="qmutex.html#4e6e5d">lock</a>();
}
mymutex.<a href="qmutex.html#da15c3">unlock</a>();
key_pressed.<a href="#3b1793">wakeAll</a>();
}
</pre>
<p>The mutexes are necessary because the results if two threads
attempt to change the value of the same variable simultaneously
are unpredictable.
<hr><h2>Member Function Documentation</h2>
<h3 class="fn"><a name="eee0a1"></a>QWaitCondition::QWaitCondition()</h3>
<p>Constructs a new event signalling object.
<h3 class="fn"><a name="ee5dc6"></a>QWaitCondition::~QWaitCondition() <code>[virtual]</code></h3>
<p>Deletes the event signalling object.
<h3 class="fn">bool<a name="e72dde"></a>QWaitCondition::wait(<a href="qmutex.html">QMutex</a>*mutex, unsignedlongtime=ULONG_MAX)</h3>
<p>Release the locked <em>mutex</em> and wait on the thread event object. The
<em>mutex</em> must be initially locked by the calling thread. If <em>mutex</em>
is not in a locked state, this function returns immediately. The
<em>mutex</em> will be unlocked, and the thread calling will block until
one of 2 conditions is met:
<ul>
<li> Another thread signals it using <a href="#57c32e">wakeOne</a>() or <a href="#3b1793">wakeAll</a>(). This
function will return TRUE in this case.
<li> <em>time</em> milliseconds has elapsed. If <em>time</em> is ULONG_MAX (default
argument), then the wait will never timeout (the event must
signalled). This function will return FALSE if the
wait timed out.
</ul>
<p>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.
<p>See also <a href="#57c32e">wakeOne</a>() and <a href="#3b1793">wakeAll</a>().
<h3 class="fn">bool<a name="d24199"></a>QWaitCondition::wait(unsignedlongtime=ULONG_MAX)</h3>
<p>Wait on the thread event object. The thread calling this will block
until one of 2 conditions is met:
<ul>
<li> Another thread signals it using <a href="#57c32e">wakeOne</a>() or <a href="#3b1793">wakeAll</a>(). This
function will return TRUE in this case.
<li> <em>time</em> milliseconds has elapsed. If <em>time</em> is ULONG_MAX (default
argument), then the wait will never timeout (the event must
signalled). This function will return FALSE if the
wait timed out.
</ul>
<p>See also <a href="#57c32e">wakeOne</a>() and <a href="#3b1793">wakeAll</a>().
<h3 class="fn">void<a name="3b1793"></a>QWaitCondition::wakeAll()</h3>
<p>This wakes all threads waiting on the QWaitCondition. The order in
which the threads are woken up depends on the operating system's
scheduling policies, and cannot be controlled or predicted.
<p>See also <a href="#57c32e">wakeOne</a>().
<h3 class="fn">void<a name="57c32e"></a>QWaitCondition::wakeOne()</h3>
<p>This wakes one thread waiting on the QWaitCondition. The thread that
woken up depends on the operating system's scheduling policies, and
cannot be controlled or predicted.
<p>See also <a href="#3b1793">wakeAll</a>().
<hr><p>
Search the documentation, FAQ, qt-interest archive and more (uses
<a href="http://www.trolltech.com">www.trolltech.com</a>):<br>
<form method=post action="http://www.trolltech.com/search.cgi">
<input type=hidden name="version" value="2.3.2"><nobr>
<input size="50" name="search"><input type=submit value="Search">
</nobr></form><hr><p>
This file is part of the <a href="index.html">Qt toolkit</a>,
copyright © 1995-2001
<a href="http://www.trolltech.com">Trolltech</a>, all rights reserved.<p><address><hr><div align="center">
<table width="100%" cellspacing="0" border="0"><tr>
<td>Copyright 2001 Trolltech<td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a>
<td align="right"><div align="right">Qt version 2.3.2</div>
</table></div></address></body></html>
|