summaryrefslogtreecommitdiffstats
path: root/doc/html/qwaitcondition.html
blob: a2735ecd27a6e968b48d21d2e77ecb866fdb49d8 (plain)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/src/tools/qwaitcondition_unix.cpp:57 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>QWaitCondition Class</title>
<style type="text/css"><!--
fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
body { background: #ffffff; color: black; }
--></style>
</head>
<body>

<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr bgcolor="#E5E5E5">
<td valign=center>
 <a href="index.html">
<font color="#004faf">Home</font></a>
 | <a href="classes.html">
<font color="#004faf">All&nbsp;Classes</font></a>
 | <a href="mainclasses.html">
<font color="#004faf">Main&nbsp;Classes</font></a>
 | <a href="annotated.html">
<font color="#004faf">Annotated</font></a>
 | <a href="groups.html">
<font color="#004faf">Grouped&nbsp;Classes</font></a>
 | <a href="functions.html">
<font color="#004faf">Functions</font></a>
</td>
<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>QWaitCondition Class Reference</h1>

<p>The QWaitCondition class allows waiting/waking for conditions between threads.
<a href="#details">More...</a>
<p>All the functions in this class are <a href="threads.html#threadsafe">thread-safe</a> when Qt is built with thread support.</p>
<p><tt>#include &lt;<a href="qwaitcondition-h.html">qwaitcondition.h</a>&gt;</tt>
<p><a href="qwaitcondition-members.html">List of all member functions.</a>
<h2>Public Members</h2>
<ul>
<li class=fn><a href="#QWaitCondition"><b>QWaitCondition</b></a> ()</li>
<li class=fn>virtual <a href="#~QWaitCondition"><b>~QWaitCondition</b></a> ()</li>
<li class=fn>bool <a href="#wait"><b>wait</b></a> ( unsigned&nbsp;long&nbsp;time = ULONG_MAX )</li>
<li class=fn>bool <a href="#wait-2"><b>wait</b></a> ( QMutex&nbsp;*&nbsp;mutex, unsigned&nbsp;long&nbsp;time = ULONG_MAX )</li>
<li class=fn>void <a href="#wakeOne"><b>wakeOne</b></a> ()</li>
<li class=fn>void <a href="#wakeAll"><b>wakeAll</b></a> ()</li>
</ul>
<hr><a name="details"></a><h2>Detailed Description</h2>



The QWaitCondition class allows waiting/waking for conditions between threads.
<p> 

<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="#wakeOne">wakeOne</a>() or
<a href="#wakeAll">wakeAll</a>(). Use wakeOne() to wake one randomly selected event or
wakeAll() 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 this:
<p> <pre>
    QWaitCondition key_pressed;

    for (;;) {
        key_pressed.<a href="#wait">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 this:
<p> <pre>
    QWaitCondition key_pressed;

    for (;;) {
        getchar();
        // Causes any thread in key_pressed.<a href="#wait">wait</a>() to return from
        // that method and continue processing
        key_pressed.<a href="#wakeAll">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;
    QWaitCondition key_pressed;
    int mycount=0;

    // Worker thread code
    for (;;) {
        key_pressed.<a href="#wait">wait</a>(); // This is a QWaitCondition global variable
        mymutex.<a href="qmutex.html#lock">lock</a>();
        mycount++;
        mymutex.<a href="qmutex.html#unlock">unlock</a>();
        do_something();
        mymutex.<a href="qmutex.html#lock">lock</a>();
        mycount--;
        mymutex.<a href="qmutex.html#unlock">unlock</a>();
    }

    // Key reading thread code
    for (;;) {
        getchar();
        mymutex.<a href="qmutex.html#lock">lock</a>();
        // Sleep until there are no busy worker threads
        while( mycount &gt; 0 ) {
            mymutex.<a href="qmutex.html#unlock">unlock</a>();
            sleep( 1 );
            mymutex.<a href="qmutex.html#lock">lock</a>();
        }
        mymutex.<a href="qmutex.html#unlock">unlock</a>();
        key_pressed.<a href="#wakeAll">wakeAll</a>();
    }
    </pre>
 
<p> The mutexes are necessary because the results of two threads
attempting to change the value of the same variable simultaneously
are unpredictable.
<p>See also <a href="environment.html">Environment Classes</a> and <a href="thread.html">Threading</a>.

<hr><h2>Member Function Documentation</h2>
<h3 class=fn><a name="QWaitCondition"></a>QWaitCondition::QWaitCondition ()
</h3>
Constructs a new event signalling, i.e. wait condition, object.

<h3 class=fn><a name="~QWaitCondition"></a>QWaitCondition::~QWaitCondition ()<tt> [virtual]</tt>
</h3>
Deletes the event signalling, i.e. wait condition, object.

<h3 class=fn>bool <a name="wait"></a>QWaitCondition::wait ( unsigned&nbsp;long&nbsp;time = ULONG_MAX )
</h3>
Wait on the thread event object. The thread calling this will
block until either of these conditions is met:
<ul>
<li> Another thread signals it using <a href="#wakeOne">wakeOne</a>() or <a href="#wakeAll">wakeAll</a>(). This
function will return TRUE in this case.
<li> <em>time</em> milliseconds has elapsed. If <em>time</em> 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.
</ul>
<p> <p>See also <a href="#wakeOne">wakeOne</a>() and <a href="#wakeAll">wakeAll</a>().

<h3 class=fn>bool <a name="wait-2"></a>QWaitCondition::wait ( <a href="qmutex.html">QMutex</a>&nbsp;*&nbsp;mutex, unsigned&nbsp;long&nbsp;time = ULONG_MAX )
</h3>
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
<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.
If <em>mutex</em> is a recursive mutex, this function returns
immediately. The <em>mutex</em> will be unlocked, and the calling thread
will block until either of these conditions is met:
<ul>
<li> Another thread signals it using <a href="#wakeOne">wakeOne</a>() or <a href="#wakeAll">wakeAll</a>(). This
function will return TRUE in this case.
<li> <em>time</em> milliseconds has elapsed. If <em>time</em> 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.
</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> <p>See also <a href="#wakeOne">wakeOne</a>() and <a href="#wakeAll">wakeAll</a>().

<h3 class=fn>void <a name="wakeAll"></a>QWaitCondition::wakeAll ()
</h3>
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> <p>See also <a href="#wakeOne">wakeOne</a>().

<h3 class=fn>void <a name="wakeOne"></a>QWaitCondition::wakeOne ()
</h3>
This wakes one thread waiting on the QWaitCondition. The thread
that is woken up depends on the operating system's scheduling
policies, and cannot be controlled or predicted.
<p> <p>See also <a href="#wakeAll">wakeAll</a>().

<!-- eof -->
<hr><p>
This file is part of the <a href="index.html">Qt toolkit</a>.
Copyright &copy; 1995-2007
<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 &copy; 2007
<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
<td align=right><div align=right>Qt 3.3.8</div>
</table></div></address></body>
</html>