File: recursive_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 (368 lines) | stat: -rw-r--r-- 12,274 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
360
361
362
363
364
365
366
367
368
<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, recursive_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 src="../../../c++boost.gif" alt="C++ Boost" width=
                    "277" height="86"></h3>
                </td>

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

                    <h2 align="center">recursive_mutex<br>
                     recursive_try_mutex<br>
                     recursive_timed_mutex</h2>
                </td>
            </tr>
        </table>
        <hr>

        <p><a href="#Introduction">Introduction</a><br>
        <a href="#Header">Header</a><br>
        <a href="#recursive_mutex Synopsis">Class recursive_mutex
        Synopsis</a><br>
        <a href="#recursive_mutex Members">Class recursive_mutex
        Members</a><br>
        <a href="#recursive_try_mutex Synopsis">Class recursive_try_mutex
        Synopsis</a><br>
        <a href="#recursive_try_mutex Members">Class recursive_try_mutex
        Members</a><br>
        <a href="#recursive_timed_mutex Synopsis">Class recursive_timed_mutex
        Synopsis</a><br>
        <a href="#recursive_timed_mutex Members">Class recursive_timed_mutex
        Members</a><br>
        <a href="#Example">Example</a></p>

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

        <p>The <code>recursive_mutex</code>, <code>recursive_try_mutex</code>
        and <code>recursive_timed_mutex</code> 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 with recursive
        locking semantics. These types should be used to synchronize access to
        shared resources when recursive locking by a single thread is likely to
        occur. A good example for this is when a class supplies &quot;internal
        synchronization&quot; to ensure <a href="definitions.html#Thread-safe">
        thread-safety</a> and a function of the class may have to call other
        functions of the class which also attempt to lock the mutex. For
        recursive locking mechanics, see <a href="mutex.html">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="#recursive_mutex Synopsis"><code>
                recursive_mutex</code></a></td>

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

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

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

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

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

                <td valign="middle"><a href="scoped_lock.html"><code>
                detail::thread::scoped_lock&lt;recursive_try_mutex&gt;<br>
                </code></a> <code><a href="scoped_try_lock.html">
                detail::thread::scoped_try_lock&lt;recursive_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=
                "#recursive_timed_mutex Synopsis">
                recursive_timed_mutex</a></code> </td>

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

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

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

                 <a href="scoped_timed_lock.html"><code>
                detail::thread::scoped_timed_lock&lt;recursive_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 <code>recursive_mutex</code>, <code>recursive_try_mutex</code>
        and <code>recursive_timed_mutex</code> employ a <code>Recursive</code>
        <a href="mutex_concept.html#LockingStrategies">locking strategy</a>, so
        attempts to recursively lock them succeed and an internal &quot;lock
        count&quot; is maintained. Attempts to unlock them by a thread that
        does not own a lock on them will result in a <a href="lock_error.html">
        lock_error</a> exception being thrown.</p>

        <p>The <code>recursive_mutex</code>, <code>recursive_try_mutex</code>
        and <code>recursive_timed_mutex</code> leave the <a href=
        "mutex_concept.html#SchedulingPolicies">scheduling policy</a> as <code>
        Unspecified</code>. 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/recursive_mutex.hpp">&lt;boost/thread/recursive_mutex.hpp&gt;</a>
</pre>

        <h2>Class <a name="recursive_mutex Synopsis">recursive_mutex
        Synopsis</a></h2>
        <hr>
<pre>
namespace boost
{
   class recursive_mutex : private <a href=
"../../utility/utility.htm">boost::noncopyable</a> // Exposition only.
        // Class recursive_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;
      
      recursive_mutex();
      ~recursive_mutex();
   };
}
</pre>

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

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

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

        <h3>Destructor</h3>
<pre>
    ~recursive_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="recursive_try_mutex Synopsis">recursive_try_mutex
        Synopsis</a></h2>
<pre>
namespace boost
{
    class recursive_try_mutex : private boost::noncopyable // Exposition only.
        // Class recursive_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;
      
       recursive_try_mutex();
       ~recursive_try_mutex();
    };
}
</pre>

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

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

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

        <h3>Destructor</h3>
<pre>
    ~recursive_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="recursive_timed_mutex Synopsis">
        recursive_timed_mutex Synopsis</a></h2>
<pre>
namespace boost
{
    class recursive_timed_mutex : private boost::noncopyable // Exposition only.
        // Class recursive_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;
      
       recursive_timed_mutex();
       ~recursive_timed_mutex();
    };
}
</pre>

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

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

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

        <h3>Destructor</h3>
<pre>
    ~recursive_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/recursive_mutex.hpp">&lt;boost/thread/recursive_mutex.hpp&gt;</a>
#include <a href=
"../../../boost/thread/thread.hpp">&lt;boost/thread/thread.hpp&gt;</a>
#include &lt;iostream&gt;

class counter
{
public:
    counter() : count(0) { }

    int add(int val) {
        boost::recursive_mutex::scoped_lock scoped_lock(mutex);
        count += val;
        return count;
    }   
    int increment() {
        boost::recursive_mutex::scoped_lock scoped_lock(mutex);
        return add(1);
    }

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

counter c;

void change_count(void*)
{
    std::cout &lt;&lt; &quot;count == &quot; &lt;&lt; c.increment() &lt;&lt; std::endl;
}

int main(int, char*[])
{
    const int num_threads=4;

    boost::thread_group threads;   
    for (int i=0; i &lt; num_threads; ++i)
        threads.create_thread(&amp;change_count, 0);
      
    threads.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>