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
198
199
200
201
202
|
'\" t
.TH TQWaitCondition 3qt "2 February 2007" "Trolltech AS" \" -*- nroff -*-
.\" Copyright 1992-2007 Trolltech ASA. All rights reserved. See the
.\" license file included in the distribution for a complete license
.\" statement.
.\"
.ad l
.nh
.SH NAME
TQWaitCondition \- Allows waiting/waking for conditions between threads
.SH SYNOPSIS
All the functions in this class are thread-safe when TQt is built with thread support.</p>
.PP
\fC#include <tqwaitcondition.h>\fR
.PP
.SS "Public Members"
.in +1c
.ti -1c
.BI "\fBTQWaitCondition\fR ()"
.br
.ti -1c
.BI "virtual \fB~TQWaitCondition\fR ()"
.br
.ti -1c
.BI "bool \fBwait\fR ( unsigned long time = ULONG_MAX )"
.br
.ti -1c
.BI "bool \fBwait\fR ( TQMutex * mutex, unsigned long time = ULONG_MAX )"
.br
.ti -1c
.BI "void \fBwakeOne\fR ()"
.br
.ti -1c
.BI "void \fBwakeAll\fR ()"
.br
.in -1c
.SH DESCRIPTION
The TQWaitCondition class allows waiting/waking for conditions between threads.
.PP
TQWaitConditions allow a thread to tell other threads that some sort of condition has been met; one or many threads can block waiting for a TQWaitCondition to set a condition with wakeOne() or wakeAll(). 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:
.PP
.nf
.br
TQWaitCondition key_pressed;
.br
.br
for (;;) {
.br
key_pressed.wait(); // This is a TQWaitCondition global variable
.br
// Key was pressed, do something interesting
.br
do_something();
.br
}
.br
.fi
.PP
A fourth thread would read key presses and wake the other three threads up every time it receives one, like this:
.PP
.nf
.br
TQWaitCondition key_pressed;
.br
.br
for (;;) {
.br
getchar();
.br
// Causes any thread in key_pressed.wait() to return from
.br
// that method and continue processing
.br
key_pressed.wakeAll();
.br
}
.br
.fi
.PP
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:
.PP
.nf
.br
TQMutex mymutex;
.br
TQWaitCondition key_pressed;
.br
int mycount=0;
.br
.br
// Worker thread code
.br
for (;;) {
.br
key_pressed.wait(); // This is a TQWaitCondition global variable
.br
mymutex.lock();
.br
mycount++;
.br
mymutex.unlock();
.br
do_something();
.br
mymutex.lock();
.br
mycount--;
.br
mymutex.unlock();
.br
}
.br
.br
// Key reading thread code
.br
for (;;) {
.br
getchar();
.br
mymutex.lock();
.br
// Sleep until there are no busy worker threads
.br
while( mycount > 0 ) {
.br
mymutex.unlock();
.br
sleep( 1 );
.br
mymutex.lock();
.br
}
.br
mymutex.unlock();
.br
key_pressed.wakeAll();
.br
}
.br
.fi
.PP
The mutexes are necessary because the results of two threads attempting to change the value of the same variable simultaneously are unpredictable.
.PP
See also Environment Classes and Threading.
.SH MEMBER FUNCTION DOCUMENTATION
.SH "TQWaitCondition::TQWaitCondition ()"
Constructs a new event signalling, i.e. wait condition, object.
.SH "TQWaitCondition::~TQWaitCondition ()\fC [virtual]\fR"
Deletes the event signalling, i.e. wait condition, object.
.SH "bool TQWaitCondition::wait ( unsigned long time = ULONG_MAX )"
Wait on the thread event object. The thread calling this will block until either of these conditions is met:
.TP
Another thread signals it using wakeOne() or wakeAll(). This function will return TRUE in this case.
.TP
\fItime\fR milliseconds has elapsed. If \fItime\fR 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.
.PP
See also wakeOne() and wakeAll().
.SH "bool TQWaitCondition::wait ( TQMutex * mutex, unsigned long time = ULONG_MAX )"
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.PP
Release the locked \fImutex\fR and wait on the thread event object. The \fImutex\fR must be initially locked by the calling thread. If \fImutex\fR is not in a locked state, this function returns immediately. If \fImutex\fR is a recursive mutex, this function returns immediately. The \fImutex\fR will be unlocked, and the calling thread will block until either of these conditions is met:
.TP
Another thread signals it using wakeOne() or wakeAll(). This function will return TRUE in this case.
.TP
\fItime\fR milliseconds has elapsed. If \fItime\fR 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.
.PP
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.
.PP
See also wakeOne() and wakeAll().
.SH "void TQWaitCondition::wakeAll ()"
This wakes all threads waiting on the TQWaitCondition. The order in which the threads are woken up depends on the operating system's scheduling policies, and cannot be controlled or predicted.
.PP
See also wakeOne().
.SH "void TQWaitCondition::wakeOne ()"
This wakes one thread waiting on the TQWaitCondition. The thread that is woken up depends on the operating system's scheduling policies, and cannot be controlled or predicted.
.PP
See also wakeAll().
.SH "SEE ALSO"
.BR http://doc.trolltech.com/tqwaitcondition.html
.BR http://www.trolltech.com/faq/tech.html
.SH COPYRIGHT
Copyright 1992-2007 Trolltech ASA, http://www.trolltech.com. See the
license file included in the distribution for a complete license
statement.
.SH AUTHOR
Generated automatically from the source code.
.SH BUGS
If you find a bug in Qt, please report it as described in
.BR http://doc.trolltech.com/bughowto.html .
Good bug reports help us to help you. Thank you.
.P
The definitive TQt documentation is provided in HTML format; it is
located at $TQTDIR/doc/html and can be read using TQt Assistant or with
a web browser. This man page is provided as a convenience for those
users who prefer man pages, although this format is not officially
supported by Trolltech.
.P
If you find errors in this manual page, please report them to
.BR [email protected] .
Please include the name of the manual page (tqwaitcondition.3qt) and the Qt
version (3.3.8).
|