Imported Upstream version 4.8.1
[platform/upstream/gcc48.git] / libstdc++-v3 / doc / html / manual / ext_concurrency_use.html
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Use</title><meta name="generator" content="DocBook XSL-NS Stylesheets V1.77.1" /><meta name="keywords" content="ISO C++, library" /><meta name="keywords" content="ISO C++, library" /><meta name="keywords" content="ISO C++, runtime, library" /><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="ext_concurrency_impl.html" title="Implementation" /><link rel="next" href="appendix.html" title="Part IV.  Appendices" /></head><body><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="ext_concurrency_impl.html">Prev</a> </td><th width="60%" align="center">Chapter 30. Concurrency</th><td width="20%" align="right"> <a accesskey="n" href="appendix.html">Next</a></td></tr></table><hr /></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="manual.ext.concurrency.use"></a>Use</h2></div></div></div><p>Typical usage of the last two constructs is demonstrated as follows:
3 </p><pre class="programlisting">
4 #include &lt;ext/concurrence.h&gt;
5
6 namespace
7 {
8   __gnu_cxx::__mutex safe_base_mutex;
9 } // anonymous namespace
10
11 namespace other
12 {
13   void
14   foo()
15   {
16     __gnu_cxx::__scoped_lock sentry(safe_base_mutex);
17     for (int i = 0; i &lt; max;  ++i)
18       {
19         _Safe_iterator_base* __old = __iter;
20         __iter = __iter-&lt;_M_next;
21         __old-&lt;_M_detach_single();
22       }
23 }
24 </pre><p>In this sample code, an anonymous namespace is used to keep
25 the <code class="code">__mutex</code> private to the compilation unit,
26 and <code class="code">__scoped_lock</code> is used to guard access to the critical
27 section within the for loop, locking the mutex on creation and freeing
28 the mutex as control moves out of this block.
29 </p><p>Several exception classes are used to keep track of
30 concurrence-related errors. These classes
31 are: <code class="code">__concurrence_lock_error</code>, <code class="code">__concurrence_unlock_error</code>, <code class="code">__concurrence_wait_error</code>,
32 and <code class="code">__concurrence_broadcast_error</code>.
33 </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ext_concurrency_impl.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="appendix.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. 
34   Appendices
35 </td></tr></table></div></body></html>