| 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> | 
|---|
| 2 | <!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/src/tools/qmutex_unix.cpp:511 --> | 
|---|
| 3 | <html> | 
|---|
| 4 | <head> | 
|---|
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> | 
|---|
| 6 | <title>QMutexLocker Class</title> | 
|---|
| 7 | <style type="text/css"><!-- | 
|---|
| 8 | fn { margin-left: 1cm; text-indent: -1cm; } | 
|---|
| 9 | a:link { color: #004faf; text-decoration: none } | 
|---|
| 10 | a:visited { color: #672967; text-decoration: none } | 
|---|
| 11 | body { background: #ffffff; color: black; } | 
|---|
| 12 | --></style> | 
|---|
| 13 | </head> | 
|---|
| 14 | <body> | 
|---|
| 15 |  | 
|---|
| 16 | <table border="0" cellpadding="0" cellspacing="0" width="100%"> | 
|---|
| 17 | <tr bgcolor="#E5E5E5"> | 
|---|
| 18 | <td valign=center> | 
|---|
| 19 | <a href="index.html"> | 
|---|
| 20 | <font color="#004faf">Home</font></a> | 
|---|
| 21 | | <a href="classes.html"> | 
|---|
| 22 | <font color="#004faf">All Classes</font></a> | 
|---|
| 23 | | <a href="mainclasses.html"> | 
|---|
| 24 | <font color="#004faf">Main Classes</font></a> | 
|---|
| 25 | | <a href="annotated.html"> | 
|---|
| 26 | <font color="#004faf">Annotated</font></a> | 
|---|
| 27 | | <a href="groups.html"> | 
|---|
| 28 | <font color="#004faf">Grouped Classes</font></a> | 
|---|
| 29 | | <a href="functions.html"> | 
|---|
| 30 | <font color="#004faf">Functions</font></a> | 
|---|
| 31 | </td> | 
|---|
| 32 | <td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>QMutexLocker Class Reference</h1> | 
|---|
| 33 |  | 
|---|
| 34 | <p>The QMutexLocker class simplifies locking and unlocking QMutexes. | 
|---|
| 35 | <a href="#details">More...</a> | 
|---|
| 36 | <p>All the functions in this class are <a href="threads.html#threadsafe">thread-safe</a> when Qt is built with thread support.</p> | 
|---|
| 37 | <p><tt>#include <<a href="qmutex-h.html">qmutex.h</a>></tt> | 
|---|
| 38 | <p><a href="qmutexlocker-members.html">List of all member functions.</a> | 
|---|
| 39 | <h2>Public Members</h2> | 
|---|
| 40 | <ul> | 
|---|
| 41 | <li class=fn><a href="#QMutexLocker"><b>QMutexLocker</b></a> ( QMutex * mutex )</li> | 
|---|
| 42 | <li class=fn><a href="#~QMutexLocker"><b>~QMutexLocker</b></a> ()</li> | 
|---|
| 43 | <li class=fn>QMutex * <a href="#mutex"><b>mutex</b></a> () const</li> | 
|---|
| 44 | </ul> | 
|---|
| 45 | <hr><a name="details"></a><h2>Detailed Description</h2> | 
|---|
| 46 |  | 
|---|
| 47 |  | 
|---|
| 48 | The QMutexLocker class simplifies locking and unlocking QMutexes. | 
|---|
| 49 | <p> | 
|---|
| 50 | <p> | 
|---|
| 51 |  | 
|---|
| 52 | <p> The purpose of QMutexLocker is to simplify <a href="qmutex.html">QMutex</a> locking and | 
|---|
| 53 | unlocking. Locking and unlocking a QMutex in complex functions and | 
|---|
| 54 | statements or in exception handling code is error prone and | 
|---|
| 55 | difficult to debug. QMutexLocker should be used in such situations | 
|---|
| 56 | to ensure that the state of the mutex is well defined and always | 
|---|
| 57 | locked and unlocked properly. | 
|---|
| 58 | <p> QMutexLocker should be created within a function where a QMutex | 
|---|
| 59 | needs to be locked. The mutex is locked when QMutexLocker is | 
|---|
| 60 | created, and unlocked when QMutexLocker is destroyed. | 
|---|
| 61 | <p> For example, this complex function locks a QMutex upon entering | 
|---|
| 62 | the function and unlocks the mutex at all the exit points: | 
|---|
| 63 | <p> <pre> | 
|---|
| 64 | int complexFunction( int flag ) | 
|---|
| 65 | { | 
|---|
| 66 | mutex.lock(); | 
|---|
| 67 |  | 
|---|
| 68 | int return_value = 0; | 
|---|
| 69 |  | 
|---|
| 70 | switch ( flag ) { | 
|---|
| 71 | case 0: | 
|---|
| 72 | case 1: | 
|---|
| 73 | { | 
|---|
| 74 | mutex.unlock(); | 
|---|
| 75 | return moreComplexFunction( flag ); | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | case 2: | 
|---|
| 79 | { | 
|---|
| 80 | int status = anotherFunction(); | 
|---|
| 81 | if ( status < 0 ) { | 
|---|
| 82 | mutex.unlock(); | 
|---|
| 83 | return -2; | 
|---|
| 84 | } | 
|---|
| 85 | return_value = status + flag; | 
|---|
| 86 | break; | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 | default: | 
|---|
| 90 | { | 
|---|
| 91 | if ( flag > 10 ) { | 
|---|
| 92 | mutex.unlock(); | 
|---|
| 93 | return -1; | 
|---|
| 94 | } | 
|---|
| 95 | break; | 
|---|
| 96 | } | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | mutex.unlock(); | 
|---|
| 100 | return return_value; | 
|---|
| 101 | } | 
|---|
| 102 | </pre> | 
|---|
| 103 |  | 
|---|
| 104 | <p> This example function will get more complicated as it is | 
|---|
| 105 | developed, which increases the likelihood that errors will occur. | 
|---|
| 106 | <p> Using QMutexLocker greatly simplifies the code, and makes it more | 
|---|
| 107 | readable: | 
|---|
| 108 | <p> <pre> | 
|---|
| 109 | int complexFunction( int flag ) | 
|---|
| 110 | { | 
|---|
| 111 | QMutexLocker locker( &mutex ); | 
|---|
| 112 |  | 
|---|
| 113 | int return_value = 0; | 
|---|
| 114 |  | 
|---|
| 115 | switch ( flag ) { | 
|---|
| 116 | case 0: | 
|---|
| 117 | case 1: | 
|---|
| 118 | { | 
|---|
| 119 | return moreComplexFunction( flag ); | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 | case 2: | 
|---|
| 123 | { | 
|---|
| 124 | int status = anotherFunction(); | 
|---|
| 125 | if ( status < 0 ) | 
|---|
| 126 | return -2; | 
|---|
| 127 | return_value = status + flag; | 
|---|
| 128 | break; | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | default: | 
|---|
| 132 | { | 
|---|
| 133 | if ( flag > 10 ) | 
|---|
| 134 | return -1; | 
|---|
| 135 | break; | 
|---|
| 136 | } | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | return return_value; | 
|---|
| 140 | } | 
|---|
| 141 | </pre> | 
|---|
| 142 |  | 
|---|
| 143 | <p> Now, the mutex will always be unlocked when the QMutexLocker | 
|---|
| 144 | object is destroyed (when the function returns since <tt>locker</tt> is | 
|---|
| 145 | an auto variable). Note that the mutex will be unlocked after | 
|---|
| 146 | the call to moreComplexFunction() in this example, avoiding | 
|---|
| 147 | possible bugs caused by unlocking the mutex too early, as in | 
|---|
| 148 | the first example. | 
|---|
| 149 | <p> The same principle applies to code that throws and catches | 
|---|
| 150 | exceptions. An exception that is not caught in the function that | 
|---|
| 151 | has locked the mutex has no way of unlocking the mutex before the | 
|---|
| 152 | exception is passed up the stack to the calling function. | 
|---|
| 153 | <p> QMutexLocker also provides a <a href="#mutex">mutex</a>() member function that returns | 
|---|
| 154 | the mutex on which the QMutexLocker is operating. This is useful | 
|---|
| 155 | for code that needs access to the mutex, such as | 
|---|
| 156 | <a href="qwaitcondition.html#wait">QWaitCondition::wait</a>(). For example: | 
|---|
| 157 | <p> <pre> | 
|---|
| 158 | class SignalWaiter | 
|---|
| 159 | { | 
|---|
| 160 | private: | 
|---|
| 161 | QMutexLocker locker; | 
|---|
| 162 |  | 
|---|
| 163 | public: | 
|---|
| 164 | SignalWaiter( <a href="qmutex.html">QMutex</a> *mutex ) | 
|---|
| 165 | : locker( mutex ) | 
|---|
| 166 | { | 
|---|
| 167 | } | 
|---|
| 168 |  | 
|---|
| 169 | void waitForSignal() | 
|---|
| 170 | { | 
|---|
| 171 | ... | 
|---|
| 172 | ... | 
|---|
| 173 | ... | 
|---|
| 174 |  | 
|---|
| 175 | while ( ! signalled ) | 
|---|
| 176 | waitcondition.wait( locker.<a href="#mutex">mutex</a>() ); | 
|---|
| 177 |  | 
|---|
| 178 | ... | 
|---|
| 179 | ... | 
|---|
| 180 | ... | 
|---|
| 181 | } | 
|---|
| 182 | }; | 
|---|
| 183 | </pre> | 
|---|
| 184 |  | 
|---|
| 185 | <p> <p>See also <a href="qmutex.html">QMutex</a>, <a href="qwaitcondition.html">QWaitCondition</a>, <a href="environment.html">Environment Classes</a>, and <a href="thread.html">Threading</a>. | 
|---|
| 186 |  | 
|---|
| 187 | <hr><h2>Member Function Documentation</h2> | 
|---|
| 188 | <h3 class=fn><a name="QMutexLocker"></a>QMutexLocker::QMutexLocker ( <a href="qmutex.html">QMutex</a> * mutex ) | 
|---|
| 189 | </h3> | 
|---|
| 190 |  | 
|---|
| 191 | <p> Constructs a QMutexLocker and locks <em>mutex</em>. The mutex will be | 
|---|
| 192 | unlocked when the QMutexLocker is destroyed. If <em>mutex</em> is zero, | 
|---|
| 193 | QMutexLocker does nothing. | 
|---|
| 194 | <p> <p>See also <a href="qmutex.html#lock">QMutex::lock</a>(). | 
|---|
| 195 |  | 
|---|
| 196 | <h3 class=fn><a name="~QMutexLocker"></a>QMutexLocker::~QMutexLocker () | 
|---|
| 197 | </h3> | 
|---|
| 198 |  | 
|---|
| 199 | <p> Destroys the QMutexLocker and unlocks the mutex which was locked | 
|---|
| 200 | in the constructor. | 
|---|
| 201 | <p> <p>See also <a href="#QMutexLocker">QMutexLocker::QMutexLocker</a>() and <a href="qmutex.html#unlock">QMutex::unlock</a>(). | 
|---|
| 202 |  | 
|---|
| 203 | <h3 class=fn><a href="qmutex.html">QMutex</a> * <a name="mutex"></a>QMutexLocker::mutex () const | 
|---|
| 204 | </h3> | 
|---|
| 205 |  | 
|---|
| 206 | <p> Returns a pointer to the mutex which was locked in the | 
|---|
| 207 | constructor. | 
|---|
| 208 | <p> <p>See also <a href="#QMutexLocker">QMutexLocker::QMutexLocker</a>(). | 
|---|
| 209 |  | 
|---|
| 210 | <!-- eof --> | 
|---|
| 211 | <hr><p> | 
|---|
| 212 | This file is part of the <a href="index.html">Qt toolkit</a>. | 
|---|
| 213 | Copyright © 1995-2007 | 
|---|
| 214 | <a href="http://www.trolltech.com/">Trolltech</a>. All Rights Reserved.<p><address><hr><div align=center> | 
|---|
| 215 | <table width=100% cellspacing=0 border=0><tr> | 
|---|
| 216 | <td>Copyright © 2007 | 
|---|
| 217 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a> | 
|---|
| 218 | <td align=right><div align=right>Qt 3.3.8</div> | 
|---|
| 219 | </table></div></address></body> | 
|---|
| 220 | </html> | 
|---|