summaryrefslogtreecommitdiffstats
path: root/doc/html/qmutexlocker.html
blob: 44eff46bada884b612daea44906e533c8958b5c5 (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<!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/qmutex_unix.cpp:511 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>QMutexLocker 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>QMutexLocker Class Reference</h1>

<p>The QMutexLocker class simplifies locking and unlocking QMutexes.
<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="qmutex-h.html">qmutex.h</a>&gt;</tt>
<p><a href="qmutexlocker-members.html">List of all member functions.</a>
<h2>Public Members</h2>
<ul>
<li class=fn><a href="#QMutexLocker"><b>QMutexLocker</b></a> ( QMutex&nbsp;*&nbsp;mutex )</li>
<li class=fn><a href="#~QMutexLocker"><b>~QMutexLocker</b></a> ()</li>
<li class=fn>QMutex * <a href="#mutex"><b>mutex</b></a> () const</li>
</ul>
<hr><a name="details"></a><h2>Detailed Description</h2>


The QMutexLocker class simplifies locking and unlocking QMutexes.
<p> 
<p> 

<p> The purpose of QMutexLocker is to simplify <a href="qmutex.html">QMutex</a> 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.
<p> 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.
<p> For example, this complex function locks a QMutex upon entering
the function and unlocks the mutex at all the exit points:
<p> <pre>
    int complexFunction( int flag )
    {
        mutex.lock();

        int return_value = 0;

        switch ( flag ) {
        case 0:
        case 1:
            {
                mutex.unlock();
                return moreComplexFunction( flag );
            }

        case 2:
            {
                int status = anotherFunction();
                if ( status &lt; 0 ) {
                    mutex.unlock();
                    return -2;
                }
                return_value = status + flag;
                break;
            }

        default:
            {
                if ( flag &gt; 10 ) {
                    mutex.unlock();
                    return -1;
                }
                break;
            }
        }

        mutex.unlock();
        return return_value;
    }
    </pre>
 
<p> This example function will get more complicated as it is
developed, which increases the likelihood that errors will occur.
<p> Using QMutexLocker greatly simplifies the code, and makes it more
readable:
<p> <pre>
    int complexFunction( int flag )
    {
        QMutexLocker locker( &amp;mutex );

        int return_value = 0;

        switch ( flag ) {
        case 0:
        case 1:
            {
                return moreComplexFunction( flag );
            }

        case 2:
            {
                int status = anotherFunction();
                if ( status &lt; 0 )
                    return -2;
                return_value = status + flag;
                break;
            }

        default:
            {
                if ( flag &gt; 10 )
                    return -1;
                break;
            }
        }

        return return_value;
    }
    </pre>
 
<p> Now, the mutex will always be unlocked when the QMutexLocker
object is destroyed (when the function returns since <tt>locker</tt> 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.
<p> 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.
<p> QMutexLocker also provides a <a href="#mutex">mutex</a>() 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
<a href="qwaitcondition.html#wait">QWaitCondition::wait</a>(). For example:
<p> <pre>
    class SignalWaiter
    {
    private:
        QMutexLocker locker;

    public:
        SignalWaiter( <a href="qmutex.html">QMutex</a> *mutex )
            : locker( mutex )
        {
        }

        void waitForSignal()
        {
            ...
            ...
            ...

            while ( ! signalled )
                waitcondition.wait( locker.<a href="#mutex">mutex</a>() );

            ...
            ...
            ...
        }
    };
    </pre>
 
<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>.

<hr><h2>Member Function Documentation</h2>
<h3 class=fn><a name="QMutexLocker"></a>QMutexLocker::QMutexLocker ( <a href="qmutex.html">QMutex</a>&nbsp;*&nbsp;mutex )
</h3>

<p> Constructs a QMutexLocker and locks <em>mutex</em>. The mutex will be
unlocked when the QMutexLocker is destroyed. If <em>mutex</em> is zero,
QMutexLocker does nothing.
<p> <p>See also <a href="qmutex.html#lock">QMutex::lock</a>().

<h3 class=fn><a name="~QMutexLocker"></a>QMutexLocker::~QMutexLocker ()
</h3>

<p> Destroys the QMutexLocker and unlocks the mutex which was locked
in the constructor.
<p> <p>See also <a href="#QMutexLocker">QMutexLocker::QMutexLocker</a>() and <a href="qmutex.html#unlock">QMutex::unlock</a>().

<h3 class=fn><a href="qmutex.html">QMutex</a>&nbsp;* <a name="mutex"></a>QMutexLocker::mutex () const
</h3>

<p> Returns a pointer to the mutex which was locked in the
constructor.
<p> <p>See also <a href="#QMutexLocker">QMutexLocker::QMutexLocker</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>