1 <html><head><meta http-equiv=
"Content-Type" content=
"text/html; charset=UTF-8"><title>Use
</title><meta name=
"generator" content=
"DocBook XSL-NS Stylesheets V1.76.1"><meta name=
"keywords" content=
"
5 "><meta name=
"keywords" content=
"
9 "><meta name=
"keywords" content=
"
15 "><link rel=
"home" href=
"../index.html" title=
"The GNU C++ Library"><link rel=
"up" href=
"ext_concurrency.html" title=
"Chapter 30. Concurrency"><link rel=
"prev" href=
"bk01pt03ch30s02.html" title=
"Implementation"><link rel=
"next" href=
"bk01pt04.html" title=
"Part IV. Appendices"></head><body bgcolor=
"white" text=
"black" link=
"#0000FF" vlink=
"#840084" alink=
"#0000FF"><div class=
"navheader"><table width=
"100%" summary=
"Navigation header"><tr><th colspan=
"3" align=
"center">Use
</th></tr><tr><td width=
"20%" align=
"left"><a accesskey=
"p" href=
"bk01pt03ch30s02.html">Prev
</a> </td><th width=
"60%" align=
"center">Chapter
30. Concurrency
</th><td width=
"20%" align=
"right"> <a accesskey=
"n" href=
"bk01pt04.html">Next
</a></td></tr></table><hr></div><div class=
"section" title=
"Use"><div class=
"titlepage"><div><div><h2 class=
"title" style=
"clear: both"><a name=
"manual.ext.concurrency.use"></a>Use
</h2></div></div></div><p>Typical usage of the last two constructs is demonstrated as follows:
16 </p><pre class=
"programlisting">
17 #include
<ext/concurrence.h
>
21 __gnu_cxx::__mutex safe_base_mutex;
22 } // anonymous namespace
29 __gnu_cxx::__scoped_lock sentry(safe_base_mutex);
30 for (int i =
0; i
< max; ++i)
32 _Safe_iterator_base* __old = __iter;
33 __iter = __iter-
<_M_next;
34 __old-
<_M_detach_single();
37 </pre><p>In this sample code, an anonymous namespace is used to keep
38 the
<code class=
"code">__mutex
</code> private to the compilation unit,
39 and
<code class=
"code">__scoped_lock
</code> is used to guard access to the critical
40 section within the for loop, locking the mutex on creation and freeing
41 the mutex as control moves out of this block.
42 </p><p>Several exception classes are used to keep track of
43 concurrence-related errors. These classes
44 are:
<code class=
"code">__concurrence_lock_error
</code>,
<code class=
"code">__concurrence_unlock_error
</code>,
<code class=
"code">__concurrence_wait_error
</code>,
45 and
<code class=
"code">__concurrence_broadcast_error
</code>.
46 </p></div><div class=
"navfooter"><hr><table width=
"100%" summary=
"Navigation footer"><tr><td width=
"40%" align=
"left"><a accesskey=
"p" href=
"bk01pt03ch30s02.html">Prev
</a> </td><td width=
"20%" align=
"center"><a accesskey=
"u" href=
"ext_concurrency.html">Up
</a></td><td width=
"40%" align=
"right"> <a accesskey=
"n" href=
"bk01pt04.html">Next
</a></td></tr><tr><td width=
"40%" align=
"left" valign=
"top">Implementation
</td><td width=
"20%" align=
"center"><a accesskey=
"h" href=
"../index.html">Home
</a></td><td width=
"40%" align=
"right" valign=
"top"> Part IV.
48 </td></tr></table></div></body></html>