File: scoped_lock.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 (227 lines) | stat: -rw-r--r-- 8,000 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
<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, scoped_lock</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">scoped_lock</h2>
                </td>
            </tr>
        </table>
        <hr>

        <p><a href="#Introduction">Introduction</a><br>
         <a href="#Header">Header</a><br>
         <a href="#Synopsis">Synopsis</a><br>
         <a href="#Members">Members</a><br>
         <a href="#Example">Example</a></p>

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

        <p>This class template defines a generic lock type which meets the <a
        href="lock_concept.html#ScopedLock">ScopedLock</a> requirements. The <a
        href="mutex.html">mutex</a>, <a href="mutex.html">try_mutex</a>, <a
        href="mutex.html">timed_mutex</a>, <a href="recursive_mutex.html">
        recursive_mutex</a>, <a href="recursive_mutex.html">
        recursive_try_mutex</a> and <a href="recursive_mutex.html">
        recursive_timed_mutex</a> classes all use this template to define their
        <code>scoped_lock</code> types.</p>

        <p>Like all the <b>Boost.Threads</b> <a href="lock_concept.html">lock
        models</a>, <code>scoped_lock</code> objects are meant to be
        short-lived. Objects of the class are not <a href=
        "definitions.html#thread-safe">thread-safe</a>, and so should not be
        shared between threads.</p>

        <p>Class <code>scoped_lock</code> follows the &quot;resource
        acquisition is initialization&quot; idiom <a href=
        "bibliography.html#Stroustrup-00">[Stroustrup 00 14.4.1]</a> and is a
        realization of the &quot;Scoped Locking Pattern&quot; <a href=
        "bibliography.html#Schmidt-00">[Schmidt-00]</a>. Thus the usage is to
        let the constructor do the locking, and then let the destructor do the
        unlocking automatically at the end of the enclosing scope. The lock()
        and unlock() members are usually not explicitly called, but are
        provided to allow for complex overlapping locks of multiple
        mutexes.</p>

        <p>The type used to instantiate the class must meet the <a href=
        "mutex_concept.html#Mutex">Mutex</a> requirements.</p>

        <p>Although this class is an implementation detail, it is publicly
        documented here because of its importance.</p>

        <h2><a name="Header">Header</a></h2>
<pre>
#include <a href=
"../../../boost/thread/detail/lock.hpp">&lt;boost/thread/detail/lock.hpp&gt;</a>
    <i>This header is usually not included directly by programmers
    because it is supplied by <a href=
"../../../boost/thread/mutex.hpp">&lt;boost/thread/mutex.hpp&gt;</a> or
    <a href=
"../../../boost/thread/recursive_mutex.hpp">&lt;boost/thread/recursive_mutex.hpp&gt;</a></i>
</pre>

        <h2><a name="Synopsis">Synopsis</a></h2>
<pre>
namespace boost { namespace detail { namespace thread {
    template &lt;typename Mutex&gt;
    class scoped_lock : private <a href=
"../../utility/utility.htm#Class noncopyable">boost::noncopyable</a> // Exposition only.
        // Class scoped_lock meets the <a href=
"overview.html#NonCopyable">NonCopyable</a> requirement.
    {
    public:
        typedef Mutex mutex_type;
        
        explicit scoped_lock(Mutex&amp; mx, bool initially_locked=true);
        ~scoped_lock();
        
        void lock();
        void unlock();
        
        operator const void*() const;
        bool locked() const;
    };
} // namespace thread
} // namespace detail
} // namespace boost
</pre>

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

        <h3>Constructor</h3>
<pre>
    explicit scoped_lock(Mutex&amp; mx, bool initially_locked=true);
</pre>

        <p><b>Effects:</b> Associates mutex <code>mx</code> with <code>
        *this</code>. If <code>initially_locked</code> is <code>true,</code>
        calls <code>lock()</code>.</p>
        <hr>

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

        <p><b>Effects:</b> If <code>locked()</code>, calls <code>
        unlock()</code>. Destroys <code>*this</code>.</p>
        <hr>

        <h3>lock</h3>
<pre>
    void lock();
</pre>

        <p><b>Effects:</b> If the associated mutex is already locked by another
        lock in the current thread, the effects depend on the locking strategy
        of the associated mutex, as shown in the following table:</p>

        <table summary="lock effects" border="1" cellpadding="5">
            <tr>
                <td><i><a href="mutex_concept.html#LockingStrategies">Locking
                Strategy</a><br>
                 of associated mutex</i></td>

                <td><i>Effect if associated mutex is already locked by the
                current thread</i></td>
            </tr>

            <tr>
                <td>Recursive</td>

                <td>As if an additional lock were added to the mutex.</td>
            </tr>

            <tr>
                <td>Checked</td>

                <td>Throws <a href="lock_error.html">lock_error</a>.</td>
            </tr>

            <tr>
                <td>Unchecked</td>

                <td>Undefined behavior [<a href="bibliography.html#ISO">ISO</a>
                1.3.12] (but typically, <a href="definitions.html#Deadlock">
                deadlock</a>.)</td>
            </tr>
        </table>

        <p>If the associated mutex is already locked by some other thread,
        places the current thread in the <a href="definitions.html#State">
        Blocked</a> state until the associated mutex is unlocked, after which
        the current thread is placed in the <a href="definitions.html#State">
        Ready</a> state, eventually to be returned to the <a href=
        "definitions.html#State">Running</a> state.</p>

        <p><b>Postcondition:</b> locked()</p>

        <p><b>Throws:</b> <a href="lock_error.html">lock_error</a> if <code>
        locked()</code> or as indicated in <b>Effects</b>.</p>
        <hr>

        <h3>unlock</h3>
<pre>
    void unlock();
</pre>

        <p><b>Effects:</b> Unlocks the associated mutex.</p>

        <p><b>Throws:</b> <a href="lock_error.html">lock_error</a> if <code>
        !locked()</code>.</p>
        <hr>

        <h3>const void* Conversion</h3>
<pre>
    operator const void*() const;
</pre>

        <p><b>Returns:</b> If the associated mutex is currently locked, a value
        convertible to <code>true</code>, else a value convertible to <code>
        false</code>.</p>

        <p><b>Rationale:</b> A <code>const void*</code> conversion is
        considered safer than a conversion to <code>bool</code>.</p>
        <hr>

        <h3>locked</h3>
<pre>
    bool locked() const;
</pre>

        <p><b>Returns:</b> <code>this-&gt;operator const void*() !=
        0</code>.</p>
        <hr>

        <h2><a name="Example">Example</a> Usage</h2>

        <p>See the example given in the documentation for the <a href=
        "mutex.html">mutex</a> class.</p>
        <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>