ext_concurrency.html revision 1.6
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>Chapter��30.��Concurrency</title><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><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="extensions.html" title="Part��III.�� Extensions" /><link rel="prev" href="ext_demangling.html" title="Chapter��29.��Demangling" /><link rel="next" href="ext_concurrency_impl.html" title="Implementation" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter��30.��Concurrency</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ext_demangling.html">Prev</a>��</td><th width="60%" align="center">Part��III.�� 3 Extensions 4 5</th><td width="20%" align="right">��<a accesskey="n" href="ext_concurrency_impl.html">Next</a></td></tr></table><hr /></div><div class="chapter"><div class="titlepage"><div><div><h2 class="title"><a id="manual.ext.concurrency"></a>Chapter��30.��Concurrency</h2></div></div></div><div class="toc"><p><strong>Table of Contents</strong></p><dl class="toc"><dt><span class="section"><a href="ext_concurrency.html#manual.ext.concurrency.design">Design</a></span></dt><dd><dl><dt><span class="section"><a href="ext_concurrency.html#manual.ext.concurrency.design.threads">Interface to Locks and Mutexes</a></span></dt><dt><span class="section"><a href="ext_concurrency.html#manual.ext.concurrency.design.atomics">Interface to Atomic Functions</a></span></dt></dl></dd><dt><span class="section"><a href="ext_concurrency_impl.html">Implementation</a></span></dt><dd><dl><dt><span class="section"><a href="ext_concurrency_impl.html#manual.ext.concurrency.impl.atomic_fallbacks">Using Built-in Atomic Functions</a></span></dt><dt><span class="section"><a href="ext_concurrency_impl.html#manual.ext.concurrency.impl.thread">Thread Abstraction</a></span></dt></dl></dd><dt><span class="section"><a href="ext_concurrency_use.html">Use</a></span></dt></dl></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="manual.ext.concurrency.design"></a>Design</h2></div></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="manual.ext.concurrency.design.threads"></a>Interface to Locks and Mutexes</h3></div></div></div><p>The file <code class="filename"><ext/concurrence.h></code> 6contains all the higher-level 7constructs for playing with threads. In contrast to the atomics layer, 8the concurrence layer consists largely of types. All types are defined within <code class="code">namespace __gnu_cxx</code>. 9</p><p> 10These types can be used in a portable manner, regardless of the 11specific environment. They are carefully designed to provide optimum 12efficiency and speed, abstracting out underlying thread calls and 13accesses when compiling for single-threaded situations (even on hosts 14that support multiple threads.) 15</p><p>The enumerated type <code class="code">_Lock_policy</code> details the set of 16available locking 17policies: <code class="code">_S_single</code>, <code class="code">_S_mutex</code>, 18and <code class="code">_S_atomic</code>. 19</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p><code class="code">_S_single</code></p><p>Indicates single-threaded code that does not need locking. 20</p></li><li class="listitem"><p><code class="code">_S_mutex</code></p><p>Indicates multi-threaded code using thread-layer abstractions. 21</p></li><li class="listitem"><p><code class="code">_S_atomic</code></p><p>Indicates multi-threaded code using atomic operations. 22</p></li></ul></div><p>The compile-time constant <code class="code">__default_lock_policy</code> is set 23to one of the three values above, depending on characteristics of the 24host environment and the current compilation flags. 25</p><p>Two more datatypes make up the rest of the 26interface: <code class="code">__mutex</code>, and <code class="code">__scoped_lock</code>. 27</p><p>The scoped lock idiom is well-discussed within the C++ 28community. This version takes a <code class="code">__mutex</code> reference, and 29locks it during construction of <code class="code">__scoped_lock</code> and 30unlocks it during destruction. This is an efficient way of locking 31critical sections, while retaining exception-safety. 32These types have been superseded in the ISO C++ 2011 standard by the 33mutex and lock types defined in the header 34<code class="filename"><mutex></code>. 35</p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="manual.ext.concurrency.design.atomics"></a>Interface to Atomic Functions</h3></div></div></div><p> 36Two functions and one type form the base of atomic support. 37</p><p>The type <code class="code">_Atomic_word</code> is a signed integral type 38supporting atomic operations. 39</p><p> 40The two functions functions are: 41</p><pre class="programlisting"> 42_Atomic_word 43__exchange_and_add_dispatch(volatile _Atomic_word*, int); 44 45void 46__atomic_add_dispatch(volatile _Atomic_word*, int); 47</pre><p>Both of these functions are declared in the header file 48<ext/atomicity.h>, and are in <code class="code">namespace __gnu_cxx</code>. 49</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p> 50<code class="code"> 51__exchange_and_add_dispatch 52</code> 53</p><p>Adds the second argument's value to the first argument. Returns the old value. 54</p></li><li class="listitem"><p> 55<code class="code"> 56__atomic_add_dispatch 57</code> 58</p><p>Adds the second argument's value to the first argument. Has no return value. 59</p></li></ul></div><p> 60These functions forward to one of several specialized helper 61functions, depending on the circumstances. For instance, 62</p><p> 63<code class="code"> 64__exchange_and_add_dispatch 65</code> 66</p><p> 67Calls through to either of: 68</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p><code class="code">__exchange_and_add</code> 69</p><p>Multi-thread version. Inlined if compiler-generated builtin atomics 70can be used, otherwise resolved at link time to a non-builtin code 71sequence. 72</p></li><li class="listitem"><p><code class="code">__exchange_and_add_single</code> 73</p><p>Single threaded version. Inlined.</p></li></ul></div><p>However, only <code class="code">__exchange_and_add_dispatch</code> 74and <code class="code">__atomic_add_dispatch</code> should be used. These functions 75can be used in a portable manner, regardless of the specific 76environment. They are carefully designed to provide optimum efficiency 77and speed, abstracting out atomic accesses when they are not required 78(even on hosts that support compiler intrinsics for atomic 79operations.) 80</p><p> 81In addition, there are two macros 82</p><p> 83<code class="code"> 84_GLIBCXX_READ_MEM_BARRIER 85</code> 86</p><p> 87<code class="code"> 88_GLIBCXX_WRITE_MEM_BARRIER 89</code> 90</p><p> 91Which expand to the appropriate write and read barrier required by the 92host hardware and operating system. 93</p></div></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ext_demangling.html">Prev</a>��</td><td width="20%" align="center"><a accesskey="u" href="extensions.html">Up</a></td><td width="40%" align="right">��<a accesskey="n" href="ext_concurrency_impl.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter��29.��Demangling��</td><td width="20%" align="center"><a accesskey="h" href="../index.html">Home</a></td><td width="40%" align="right" valign="top">��Implementation</td></tr></table></div></body></html>