File: mutex.html

package info (click to toggle)
boost 1.27.0-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 19,908 kB
  • ctags: 26,546
  • sloc: cpp: 122,225; ansic: 10,956; python: 4,412; sh: 855; yacc: 803; makefile: 257; perl: 165; lex: 90; csh: 6
file content (359 lines) | stat: -rw-r--r-- 11,788 bytes parent folder | download
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<html>
    <head>
        <meta http-equiv="Content-Type" content=
        "text/html; charset=iso-8859-1">
        <meta name="keywords" content="threads, BTL, thread library, C++">
        <link rel="stylesheet" type="text/css" href="styles.css">

        <title>Boost.Threads, mutex</title>
    </head>

    <body bgcolor="#ffffff" link="#0000ff" vlink="#800080">
        <table summary="header" border="0" cellpadding="7" cellspacing="0"
        width="100%">
            <tr>
                <td valign="top" width="300">
                    <h3><img alt="C++ Boost" src="../../../c++boost.gif" width=
                    "277" height="86"></h3>
                </td>

                <td valign="top">
                    <h1 align="center">Boost.Threads</h1>

                    <h2 align="center">mutex<br>
                     try_mutex<br>
                     timed_mutex</h2>
                </td>
            </tr>
        </table>
        <hr>

        <p><a href="#Introduction">Introduction</a><br>
         <a href="#Header">Header</a><br>
         <a href="#mutex Synopsis">Class mutex Synopsis</a><br>
         <a href="#mutex Members">Class mutex Members</a><br>
         <a href="#try_mutex Synopsis">Class try_mutex Synopsis</a><br>
         <a href="#try_mutex Members">Class try_mutex Members</a><br>
         <a href="#timed_mutex Synopsis">Class timed_mutex Synopsis</a><br>
         <a href="#timed_mutex Members">Class timed_mutex Members</a><br>
         <a href="#Example">Example</a></p>

        <h2><a name="Introduction">Introduction</a></h2>

        <p>The <tt><a href="#mutex Synopsis">mutex</a></tt>, <tt><a href=
        "#try_mutex Synopsis">try_mutex</a></tt> and <tt><a href=
        "#timed_mutex Synopsis">timed_mutex</a></tt> classes define full
        featured models of the <a href="mutex_concept.html#Mutex">Mutex</a>, <a
        href="mutex_concept.html#TryMutex">TryMutex</a>, and <a href=
        "mutex_concept.html#TimedMutex">TimedMutex</a> concepts. These types
        should be used to non-recursively synchronize access to shared
        resources. For recursive locking mechanics, see <a href=
        "recursive_mutex.html">recursive mutexes</a>.</p>

        <p>Each class supplies one or more typedefs for lock types which model
        matching lock concepts. For the best possible performance you should
        use the mutex class that supports the minimum set of lock types that
        you need.</p>

        <table summary="lock types" border="1" cellpadding="5">
            <tr>
                <td><b>Mutex Class</b></td>

                <td><b>Lock name</b></td>

                <td><b>Implementation defined Lock Type</b></td>

                <td><b>Lock Concept</b></td>
            </tr>

            <tr>
                <td valign="top"><a href="#mutex Synopsis"><code>
                mutex</code></a></td>

                <td valign="middle"><code>scoped_lock</code></td>

                <td valign="middle"><code><a href="scoped_lock.html">
                boost::</a></code><a href=
                "scoped_lock.html"><code>detail::thread::scoped_lock&lt;mutex&gt;</code></a></td>

                <td valign="middle"><a href="lock_concept.html#ScopedLock">
                ScopedLock</a></td>
            </tr>

            <tr>
                <td valign="top"><tt><a href="#try_mutex Synopsis">
                try_mutex</a></tt> </td>

                <td valign="middle"><code>scoped_lock<br>
                 scoped_try_lock</code></td>

                <td valign="middle"><code><a href="scoped_lock.html">
                boost::</a></code><a href=
                "scoped_lock.html"><code>detail::thread::scoped_lock&lt;try_mutex&gt;<br>

                </code></a> <code><a href="scoped_try_lock.html">
                boost::detail::thread::scoped_try_lock&lt;try_mutex&gt;</a></code></td>

                <td valign="middle"><a href="lock_concept.html#ScopedLock">
                ScopedLock</a><br>
                 <a href="lock_concept.html#ScopedTryLock">
                ScopedTryLock</a></td>
            </tr>

            <tr>
                <td valign="top"><code><a href="#timed_mutex Synopsis">
                timed_mutex</a></code> </td>

                <td valign="middle"><code>scoped_lock<br>
                 scoped_try_lock<br>
                 scoped_timed_lock</code></td>

                <td valign="middle"><code><a href="scoped_lock.html">
                boost::</a></code><a href=
                "scoped_lock.html"><code>detail::thread::scoped_lock&lt;timed_mutex&gt;</code></a><br>

                 <code><a href="scoped_try_lock.html">boost::</a></code><a
                href=
                "scoped_try_lock.html"><code>detail::thread::scoped_try_lock&lt;timed_mutex&gt;</code></a><br>

                 <code><a href="scoped_timed_lock.html">boost::</a></code><a
                href=
                "scoped_timed_lock.html"><code>detail::thread::scoped_timed_lock&lt;timed_mutex&gt;</code></a></td>

                <td valign="middle"><a href="lock_concept.html#ScopedLock">
                ScopedLock</a><br>
                 <a href="lock_concept.html#ScopedTryLock">
                ScopedTryLock</a><br>
                 <a href="lock_concept.html#ScopedTimedLock">
                ScopedTimedLock</a></td>
            </tr>
        </table>

        <p>The <tt>mutex</tt>, <tt>try_mutex</tt> and <tt>timed_mutex</tt>
        classes use an <tt>Unspecified</tt> <a href=
        "mutex_concept.html#LockingStrategies">locking strategy</a>, so
        attempts to recursively lock them or attempts to unlock them by threads
        that don&#39;t own a lock on them result in <b>undefined behavior</b>.
        This strategy allows implementations to be as efficient as possible on
        any given platform. It is, however, recommended that implementations
        include debugging support to detect misuse when <tt>NDEBUG</tt> is not
        defined.</p>

        <p>Like all the <b>Boost.Threads</b> <a href="mutex_concept.html">mutex
        models</a>, the <tt>mutex</tt>, <tt>try_mutex</tt> and <tt>
        timed_mutex</tt> leave the <a href=
        "mutex_concept.html#SchedulingPolicies">scheduling policy</a> as <tt>
        Unspecified</tt>. Programmers should assume that threads waiting for a
        lock on objects of these types acquire the lock in a random order, even
        though the specific behavior for a given platform may be different.</p>

        <h2><a name="Header">Header</a></h2>
<pre>
#include <a href=
"../../../boost/thread/mutex.hpp">&lt;boost/thread/mutex.hpp&gt;</a>
</pre>

        <h2>Class <a name="mutex Synopsis">mutex Synopsis</a></h2>
<pre>
namespace boost
{
    class mutex : private <a href=
"../../utility/utility.htm">boost::noncopyable</a> // Exposition only.
        // Class mutex meets the <a href=
"overview.html#NonCopyable">NonCopyable</a> requirement.
    {
    public:
        typedef <i>[implementation defined; see <a href=
"#Introduction">Introduction</a>]</i> scoped_lock;
        
        mutex();
        ~mutex();
    };
}
</pre>

        <h2>Class <a name="mutex Members">mutex Members</a></h2>
        <hr>

        <h3>Constructor</h3>
<pre>
    mutex();
</pre>

        <p><b>Postconditions:</b> <code>*this</code> is in the unlocked
        state.</p>
        <hr>

        <h3>Destructor</h3>
<pre>
    ~mutex();
</pre>

        <p><b>Requires:</b> <code>*this</code> is in the unlocked state.</p>

        <p><b>Effects:</b> Destroys <code>*this</code>.</p>

        <p><b>Dangers:</b> Destruction of a locked mutex is a serious
        programming error resulting in undefined behavior such as a program
        crash.</p>
        <hr>

        <h2>Class <a name="try_mutex Synopsis">try_mutex Synopsis</a></h2>
<pre>
namespace boost
{
    class try_mutex : private boost::noncopyable // Exposition only.
        // Class try_mutex meets the <a href=
"overview.html#NonCopyable">NonCopyable</a> requirement.
    {
    public:
        typedef <i>[implementation defined; see <a href=
"#Introduction">Introduction</a>]</i> scoped_lock;
        typedef <i>[implementation defined; see <a href=
"#Introduction">Introduction</a>]</i> scoped_try_lock;
        
        try_mutex();
        ~try_mutex();
    };
}
</pre>

        <h2>Class <a name="try_mutex Members">try_mutex Members</a></h2>
        <hr>

        <h3>Constructor</h3>
<pre>
    try_mutex();
</pre>

        <p><b>Postconditions:</b> <code>*this</code> is in the unlocked
        state.</p>
        <hr>

        <h3>Destructor</h3>
<pre>
    ~try_mutex();
</pre>

        <p><b>Requires:</b> <code>*this</code> is in the unlocked state.</p>

        <p><b>Effects:</b> Destroys <code>*this</code>.</p>

        <p><b>Dangers:</b> Destruction of a locked mutex is a serious
        programming error resulting in undefined behavior such as a program
        crash.</p>
        <hr>

        <h2>Class <a name="timed_mutex Synopsis">timed_mutex Synopsis</a></h2>
<pre>
namespace boost
{
    class timed_mutex : private boost::noncopyable // Exposition only.
        // Class timed_mutex meets the <a href=
"overview.html#NonCopyable">NonCopyable</a> requirement.
    {
    public:
        typedef <i>[implementation defined; see <a href=
"#Introduction">Introduction</a>]</i> scoped_lock;
        typedef <i>[implementation defined; see <a href=
"#Introduction">Introduction</a>]</i> scoped_try_lock;
        typedef <i>[implementation defined; see <a href=
"#Introduction">Introduction</a>]</i> scoped_timed_lock;
        
        timed_mutex();
        ~timed_mutex();
    };
}
</pre>

        <h2>Class <a name="timed_mutex Members">timed_mutex Members</a></h2>
        <hr>

        <h3>Constructor</h3>
<pre>
    timed_mutex();
</pre>

        <p><b>Postconditions:</b> <code>*this</code> is in the unlocked
        state.</p>
        <hr>

        <h3>Destructor</h3>
<pre>
    ~timed_mutex();
</pre>

        <p><b>Requires:</b> <code>*this</code> is in the unlocked state.</p>

        <p><b>Effects:</b> Destroys <code>*this</code>.</p>

        <p><b>Dangers:</b> Destruction of a locked mutex is a serious
        programming error resulting in undefined behavior such as a program
        crash.</p>
        <hr>

        <h2><a name="Example">Example</a> Usage</h2>
<pre>
#include <a href=
"../../../boost/thread/mutex.hpp">&lt;boost/thread/mutex.hpp&gt;</a>
#include <a href=
"../../../boost/thread/thread.hpp">&lt;boost/thread/thread.hpp&gt;</a>
#include &lt;iostream&gt;

boost::mutex io_mutex; // The iostreams are not guaranteed to be <a href=
"definitions.html#Thread-safe">thread-safe</a>!

class counter
{
public:
    counter() : count(0) { }
   
    int increment() {
        boost::mutex::scoped_lock scoped_lock(mutex);
        return ++count;
    }

private:
    boost::mutex mutex;
    int count;
};

counter c;

void change_count(void*)
{
    int i = c.increment();
    boost::mutex::scoped_lock scoped_lock(io_mutex);
    std::cout &lt;&lt; &quot;count == &quot; &lt;&lt; i &lt;&lt; std::endl;
}

int main(int, char*[])
{
    const int num_threads = 4;
    boost::thread_group thrds;
    for (int i=0; i &lt; num_threads; ++i)
        thrds.create_thread(&amp;change_count, 0);
      
    thrds.join_all();
   
    return 0;
}
</pre>

        <p>The output is:</p>
<pre>
count == 1
count == 2
count == 3
count == 4
</pre>
        <hr>

        <p>Revised 
        <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->05 November, 2001<!--webbot bot="Timestamp" endspan i-checksum="39359" --></p>

        <p><i>&copy; Copyright <a href="mailto:williamkempf@hotmail.com">
        William E. Kempf</a> 2001 all rights reserved.</i></p>
    </body>
</html>