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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
'\" t
.TH QMutexLocker 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
QMutexLocker \- Simplifies locking and unlocking QMutexes
.SH SYNOPSIS
All the functions in this class are thread-safe when Qt is built with thread support.</p>
.PP
\fC#include <ntqmutex.h>\fR
.PP
.SS "Public Members"
.in +1c
.ti -1c
.BI "\fBQMutexLocker\fR ( QMutex * mutex )"
.br
.ti -1c
.BI "\fB~QMutexLocker\fR ()"
.br
.ti -1c
.BI "QMutex * \fBmutex\fR () const"
.br
.in -1c
.SH DESCRIPTION
The QMutexLocker class simplifies locking and unlocking QMutexes.
.PP
The purpose of QMutexLocker is to simplify QMutex locking and unlocking. Locking and unlocking a QMutex in complex functions and statements or in exception handling code is error prone and difficult to debug. QMutexLocker should be used in such situations to ensure that the state of the mutex is well defined and always locked and unlocked properly.
.PP
QMutexLocker should be created within a function where a QMutex needs to be locked. The mutex is locked when QMutexLocker is created, and unlocked when QMutexLocker is destroyed.
.PP
For example, this complex function locks a QMutex upon entering the function and unlocks the mutex at all the exit points:
.PP
.nf
.br
int complexFunction( int flag )
.br
{
.br
mutex.lock();
.br
.br
int return_value = 0;
.br
.br
switch ( flag ) {
.br
case 0:
.br
case 1:
.br
{
.br
mutex.unlock();
.br
return moreComplexFunction( flag );
.br
}
.br
.br
case 2:
.br
{
.br
int status = anotherFunction();
.br
if ( status < 0 ) {
.br
mutex.unlock();
.br
return -2;
.br
}
.br
return_value = status + flag;
.br
break;
.br
}
.br
.br
default:
.br
{
.br
if ( flag > 10 ) {
.br
mutex.unlock();
.br
return -1;
.br
}
.br
break;
.br
}
.br
}
.br
.br
mutex.unlock();
.br
return return_value;
.br
}
.br
.fi
.PP
This example function will get more complicated as it is developed, which increases the likelihood that errors will occur.
.PP
Using QMutexLocker greatly simplifies the code, and makes it more readable:
.PP
.nf
.br
int complexFunction( int flag )
.br
{
.br
QMutexLocker locker( &mutex );
.br
.br
int return_value = 0;
.br
.br
switch ( flag ) {
.br
case 0:
.br
case 1:
.br
{
.br
return moreComplexFunction( flag );
.br
}
.br
.br
case 2:
.br
{
.br
int status = anotherFunction();
.br
if ( status < 0 )
.br
return -2;
.br
return_value = status + flag;
.br
break;
.br
}
.br
.br
default:
.br
{
.br
if ( flag > 10 )
.br
return -1;
.br
break;
.br
}
.br
}
.br
.br
return return_value;
.br
}
.br
.fi
.PP
Now, the mutex will always be unlocked when the QMutexLocker object is destroyed (when the function returns since \fClocker\fR is an auto variable). Note that the mutex will be unlocked after the call to moreComplexFunction() in this example, avoiding possible bugs caused by unlocking the mutex too early, as in the first example.
.PP
The same principle applies to code that throws and catches exceptions. An exception that is not caught in the function that has locked the mutex has no way of unlocking the mutex before the exception is passed up the stack to the calling function.
.PP
QMutexLocker also provides a mutex() member function that returns the mutex on which the QMutexLocker is operating. This is useful for code that needs access to the mutex, such as QWaitCondition::wait(). For example:
.PP
.nf
.br
class SignalWaiter
.br
{
.br
private:
.br
QMutexLocker locker;
.br
.br
public:
.br
SignalWaiter( QMutex *mutex )
.br
: locker( mutex )
.br
{
.br
}
.br
.br
void waitForSignal()
.br
{
.br
...
.br
...
.br
...
.br
.br
while ( ! signalled )
.br
waitcondition.wait( locker.mutex() );
.br
.br
...
.br
...
.br
...
.br
}
.br
};
.br
.fi
.PP
See also QMutex, QWaitCondition, Environment Classes, and Threading.
.SH MEMBER FUNCTION DOCUMENTATION
.SH "QMutexLocker::QMutexLocker ( QMutex * mutex )"
Constructs a QMutexLocker and locks \fImutex\fR. The mutex will be unlocked when the QMutexLocker is destroyed. If \fImutex\fR is zero, QMutexLocker does nothing.
.PP
See also QMutex::lock().
.SH "QMutexLocker::~QMutexLocker ()"
Destroys the QMutexLocker and unlocks the mutex which was locked in the constructor.
.PP
See also QMutexLocker::QMutexLocker() and QMutex::unlock().
.SH "QMutex * QMutexLocker::mutex () const"
Returns a pointer to the mutex which was locked in the constructor.
.PP
See also QMutexLocker::QMutexLocker().
.SH "SEE ALSO"
.BR http://doc.trolltech.com/qmutexlocker.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 Qt documentation is provided in HTML format; it is
located at $QTDIR/doc/html and can be read using Qt 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 qt-bugs@trolltech.com .
Please include the name of the manual page (tqmutexlocker.3qt) and the Qt
version (3.3.8).
|