1QMutexLocker(3qt)                                            QMutexLocker(3qt)
2
3
4

NAME

6       QMutexLocker - Simplifies locking and unlocking QMutexes
7

SYNOPSIS

9       All the functions in this class are thread-safe when Qt is built with
10       thread support.</p>
11
12       #include <qmutex.h>
13
14   Public Members
15       QMutexLocker ( QMutex * mutex )
16       ~QMutexLocker ()
17       QMutex * mutex () const
18

DESCRIPTION

20       The QMutexLocker class simplifies locking and unlocking QMutexes.
21
22       The purpose of QMutexLocker is to simplify QMutex locking and
23       unlocking. Locking and unlocking a QMutex in complex functions and
24       statements or in exception handling code is error prone and difficult
25       to debug. QMutexLocker should be used in such situations to ensure that
26       the state of the mutex is well defined and always locked and unlocked
27       properly.
28
29       QMutexLocker should be created within a function where a QMutex needs
30       to be locked. The mutex is locked when QMutexLocker is created, and
31       unlocked when QMutexLocker is destroyed.
32
33       For example, this complex function locks a QMutex upon entering the
34       function and unlocks the mutex at all the exit points:
35
36           int complexFunction( int flag )
37           {
38               mutex.lock();
39               int return_value = 0;
40               switch ( flag ) {
41               case 0:
42               case 1:
43                   {
44                       mutex.unlock();
45                       return moreComplexFunction( flag );
46                   }
47               case 2:
48                   {
49                       int status = anotherFunction();
50                       if ( status < 0 ) {
51                           mutex.unlock();
52                           return -2;
53                       }
54                       return_value = status + flag;
55                       break;
56                   }
57               default:
58                   {
59                       if ( flag > 10 ) {
60                           mutex.unlock();
61                           return -1;
62                       }
63                       break;
64                   }
65               }
66               mutex.unlock();
67               return return_value;
68           }
69
70       This example function will get more complicated as it is developed,
71       which increases the likelihood that errors will occur.
72
73       Using QMutexLocker greatly simplifies the code, and makes it more
74       readable:
75
76           int complexFunction( int flag )
77           {
78               QMutexLocker locker( &mutex );
79               int return_value = 0;
80               switch ( flag ) {
81               case 0:
82               case 1:
83                   {
84                       return moreComplexFunction( flag );
85                   }
86               case 2:
87                   {
88                       int status = anotherFunction();
89                       if ( status < 0 )
90                           return -2;
91                       return_value = status + flag;
92                       break;
93                   }
94               default:
95                   {
96                       if ( flag > 10 )
97                           return -1;
98                       break;
99                   }
100               }
101               return return_value;
102           }
103
104       Now, the mutex will always be unlocked when the QMutexLocker object is
105       destroyed (when the function returns since locker is an auto variable).
106       Note that the mutex will be unlocked after the call to
107       moreComplexFunction() in this example, avoiding possible bugs caused by
108       unlocking the mutex too early, as in the first example.
109
110       The same principle applies to code that throws and catches exceptions.
111       An exception that is not caught in the function that has locked the
112       mutex has no way of unlocking the mutex before the exception is passed
113       up the stack to the calling function.
114
115       QMutexLocker also provides a mutex() member function that returns the
116       mutex on which the QMutexLocker is operating. This is useful for code
117       that needs access to the mutex, such as QWaitCondition::wait(). For
118       example:
119
120           class SignalWaiter
121           {
122           private:
123               QMutexLocker locker;
124           public:
125               SignalWaiter( QMutex *mutex )
126                   : locker( mutex )
127               {
128               }
129               void waitForSignal()
130               {
131                   ...
132                   ...
133                   ...
134                   while ( ! signalled )
135                       waitcondition.wait( locker.mutex() );
136                   ...
137                   ...
138                   ...
139               }
140           };
141
142       See also QMutex, QWaitCondition, Environment Classes, and Threading.
143

MEMBER FUNCTION DOCUMENTATION

QMutexLocker::QMutexLocker ( QMutex * mutex )

146       Constructs a QMutexLocker and locks mutex. The mutex will be unlocked
147       when the QMutexLocker is destroyed. If mutex is zero, QMutexLocker does
148       nothing.
149
150       See also QMutex::lock().
151

QMutexLocker::~QMutexLocker ()

153       Destroys the QMutexLocker and unlocks the mutex which was locked in the
154       constructor.
155
156       See also QMutexLocker::QMutexLocker() and QMutex::unlock().
157

QMutex * QMutexLocker::mutex () const

159       Returns a pointer to the mutex which was locked in the constructor.
160
161       See also QMutexLocker::QMutexLocker().
162
163

SEE ALSO

165       http://doc.trolltech.com/qmutexlocker.html
166       http://www.trolltech.com/faq/tech.html
167
169       Copyright 1992-2007 Trolltech ASA, http://www.trolltech.com.  See the
170       license file included in the distribution for a complete license
171       statement.
172

AUTHOR

174       Generated automatically from the source code.
175

BUGS

177       If you find a bug in Qt, please report it as described in
178       http://doc.trolltech.com/bughowto.html.  Good bug reports help us to
179       help you. Thank you.
180
181       The definitive Qt documentation is provided in HTML format; it is
182       located at $QTDIR/doc/html and can be read using Qt Assistant or with a
183       web browser. This man page is provided as a convenience for those users
184       who prefer man pages, although this format is not officially supported
185       by Trolltech.
186
187       If you find errors in this manual page, please report them to qt-
188       bugs@trolltech.com.  Please include the name of the manual page
189       (qmutexlocker.3qt) and the Qt version (3.3.8).
190
191
192
193Trolltech AS                    2 February 2007              QMutexLocker(3qt)
Impressum