locking.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0.1 Transitional//EN">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  5. <title>Boost.Flyweight Documentation - Locking policies reference</title>
  6. <link rel="stylesheet" href="../style.css" type="text/css">
  7. <link rel="start" href="../index.html">
  8. <link rel="prev" href="factories.html">
  9. <link rel="up" href="index.html">
  10. <link rel="next" href="tracking.html">
  11. </head>
  12. <body>
  13. <h1><img src="../../../../boost.png" alt="Boost logo" align=
  14. "middle" width="277" height="86">Boost.Flyweight
  15. Locking policies reference</h1>
  16. <div class="prev_link"><a href="holders.html"><img src="../prev.gif" alt="holders" border="0"><br>
  17. Holders
  18. </a></div>
  19. <div class="up_link"><a href="index.html"><img src="../up.gif" alt="Boost.Flyweight reference" border="0"><br>
  20. Boost.Flyweight reference
  21. </a></div>
  22. <div class="next_link"><a href="tracking.html"><img src="../next.gif" alt="tracking policies" border="0"><br>
  23. Tracking policies
  24. </a></div><br clear="all" style="clear: all;">
  25. <hr>
  26. <h2>Contents</h2>
  27. <ul>
  28. <li><a href="#preliminary">Preliminary concepts</a></li>
  29. <li><a href="#locking">Locking policies</a></li>
  30. <li><a href="#locking_tag_synopsis">Header
  31. <code>"boost/flyweight/locking_tag.hpp"</code> synopsis</a>
  32. <ul>
  33. <li><a href="#is_locking">Class template <code>is_locking</code></a></li>
  34. <li><a href="#locking_construct">Class template <code>locking</code></a></li>
  35. </ul>
  36. </li>
  37. <li><a href="#simple_locking_fwd_synopsis">Header
  38. <code>"boost/flyweight/simple_locking_fwd.hpp"</code> synopsis</a>
  39. </li>
  40. <li><a href="#simple_locking_synopsis">Header
  41. <code>"boost/flyweight/simple_locking.hpp"</code> synopsis</a>
  42. <ul>
  43. <li><a href="#simple_locking">Class <code>simple_locking</code></a></li>
  44. </ul>
  45. </li>
  46. <li><a href="#no_locking_fwd_synopsis">Header
  47. <code>"boost/flyweight/no_locking_fwd.hpp"</code> synopsis</a>
  48. </li>
  49. <li><a href="#no_locking_synopsis">Header
  50. <code>"boost/flyweight/no_locking.hpp"</code> synopsis</a>
  51. <ul>
  52. <li><a href="#no_locking">Class <code>no_locking</code></a></li>
  53. </ul>
  54. </li>
  55. </ul>
  56. <h2><a name="preliminary">Preliminary concepts</a></h2>
  57. <p>
  58. A <i>mutex</i> is a type whose objects can be in either of two states, called
  59. locked and unlocked, with the property that when a thread A has locked a
  60. mutex <code>m</code> and a different thread B tries to lock <code>m</code>,
  61. B is blocked until A unlocks <code>m</code>. Additionally, a mutex is said to
  62. support <i>recursive locking</i> if a thread can succesfully invoke the locking
  63. operation for a mutex already locked by this same thread; in this case, it is
  64. required that the thread execute as many unlock operations as lock
  65. operations it has performed for the mutex to become effectively unlocked.
  66. A <i>scoped lock</i> is a
  67. type associated to some mutex type whose objects do the locking/unlocking
  68. of a mutex on construction/destruction time.
  69. </p>
  70. <p>
  71. In the following table, <code>Mutex</code> is a mutex type, <code>m</code>
  72. is an object of type <code>Mutex</code>, <code>Lock</code> is a scoped lock
  73. associated to <code>Mutex</code> and <code>lk</code> is a value of
  74. <code>Lock</code>.
  75. <p align="center">
  76. <table cellspacing="0">
  77. <caption><b>Mutex and Scoped Lock requirements.</b></caption>
  78. <tr>
  79. <th align="center">expression</th>
  80. <th align="center">return type</th>
  81. <th align="center">assertion/note<br>pre/post-condition</th>
  82. </tr>
  83. <tr>
  84. <td><code>Mutex m;</code></td>
  85. <td>&nbsp;</td>
  86. <td>Post: <code>m</code> is unlocked.
  87. </td>
  88. </tr>
  89. <tr class="odd_tr">
  90. <td><code>(&amp;m)->~Mutex();</code></td>
  91. <td><code>void</code></td>
  92. <td>Pre: <code>m</code> is unlocked.</td>
  93. </tr>
  94. <tr>
  95. <td><code>Lock lk(m);</code></td>
  96. <td>&nbsp;</td>
  97. <td>Associates <code>m</code> to <code>lk</code> and locks <code>m</code>.</td>
  98. </tr>
  99. <tr class="odd_tr">
  100. <td><code>(&amp;lk)->~Lock();</code></td>
  101. <td><code>void</code></td>
  102. <td>Unlocks the mutex associated to <code>lk</code>.</td>
  103. </tr>
  104. </table>
  105. </p>
  106. <p>
  107. These concepts are very similar, but not entirely equivalent, to
  108. the homonym ones described in the
  109. <a href="../../../../doc/html/thread/synchronization.html#thread.synchronization.mutex_concepts">Boost Thread
  110. Library</a>.
  111. </p>
  112. <h2><a name="locking">Locking policies</a></h2>
  113. <p>
  114. <i>Locking policies</i> describe a mutex type and an associated
  115. scoped lock type.
  116. <a href="flyweight.html#flyweight"><code>flyweight</code></a> uses a given locking
  117. policy to synchronize the access to its internal
  118. <a href="factories.html#factory">factory</a>.
  119. </p>
  120. <p>
  121. A type <code>Locking</code> is a locking policy if:
  122. <ul>
  123. <li>One of the following conditions is satisfied:
  124. <ol type="a">
  125. <li><a href="#is_locking"><code>is_locking&lt;Locking&gt;::type</code></a> is
  126. <a href="../../../mpl/doc/refmanual/bool.html"><code>boost::mpl::true_</code></a>,</li>
  127. <li><code>Locking</code> is of the form
  128. <a href="#locking_construct"><code>locking&lt;Locking'&gt;</code></a>.</li>
  129. </ol>
  130. </li>
  131. <li>The type <code>Locking::mutex_type</code> (or
  132. <code>Locking'::mutex_type</code> if (b) applies) is a
  133. model of <a href="#preliminary"><code>Mutex</code></a>
  134. and supports recursive locking.
  135. </li>
  136. <li>The type <code>Locking::lock_type</code> (or
  137. <code>Locking'::lock_type</code> if (b) applies) is a
  138. <a href="#preliminary"><code>Scoped Lock</code></a> of
  139. the mutex referred to above.
  140. </li>
  141. </ul>
  142. </p>
  143. <h2><a name="locking_tag_synopsis">Header
  144. <a href="../../../../boost/flyweight/locking_tag.hpp"><code>"boost/flyweight/locking_tag.hpp"</code></a> synopsis</a></h2>
  145. <blockquote><pre>
  146. <span class=keyword>namespace</span> <span class=identifier>boost</span><span class=special>{</span>
  147. <span class=keyword>namespace</span> <span class=identifier>flyweights</span><span class=special>{</span>
  148. <span class=keyword>struct</span> <span class=identifier>locking_marker</span><span class=special>;</span>
  149. <span class=keyword>template</span><span class=special>&lt;</span><span class=keyword>typename</span> <span class=identifier>T</span><span class=special>&gt;</span>
  150. <span class=keyword>struct</span> <span class=identifier>is_locking</span>
  151. <span class=keyword>template</span><span class=special>&lt;</span><span class=keyword>typename</span> <span class=identifier>T</span><span class=special>&gt;</span>
  152. <span class=keyword>struct</span> <span class=identifier>locking</span><span class=special>;</span>
  153. <span class=special>}</span> <span class=comment>// namespace boost::flyweights</span>
  154. <span class=special>}</span> <span class=comment>// namespace boost</span>
  155. </pre></blockquote>
  156. <h3><a name="is_locking">Class template <code>is_locking</code></a></h3>
  157. <p>
  158. Unless specialized by the user, <code>is_locking&lt;T&gt;::type</code> is
  159. <a href="../../../mpl/doc/refmanual/bool.html"><code>boost::mpl::true_</code></a>
  160. if <code>T</code> is derived from <code>locking_marker</code>, and it is
  161. <a href="../../../mpl/doc/refmanual/bool.html"><code>boost::mpl::false_</code></a>
  162. otherwise.
  163. </p>
  164. <h3><a name="locking_construct">Class template <code>locking</code></a></h3>
  165. <p>
  166. <code>locking&lt;T&gt;</code> is a syntactic construct meant to indicate
  167. that <code>T</code> is a locking policy without resorting to the
  168. mechanisms provided by the <code>is_locking</code> class template.
  169. </p>
  170. <h2><a name="simple_locking_fwd_synopsis">Header
  171. <a href="../../../../boost/flyweight/simple_locking_fwd.hpp"><code>"boost/flyweight/simple_locking_fwd.hpp"</code></a> synopsis</a></h2>
  172. <blockquote><pre>
  173. <span class=keyword>namespace</span> <span class=identifier>boost</span><span class=special>{</span>
  174. <span class=keyword>namespace</span> <span class=identifier>flyweights</span><span class=special>{</span>
  175. <span class=keyword>struct</span> <span class=identifier>simple_locking</span><span class=special>;</span>
  176. <span class=special>}</span> <span class=comment>// namespace boost::flyweights</span>
  177. <span class=special>}</span> <span class=comment>// namespace boost</span>
  178. </pre></blockquote>
  179. <p>
  180. <code>simple_locking_fwd.hpp</code> forward declares the class
  181. <a href="#simple_locking"><code>simple_locking</code></a>.
  182. </p>
  183. <h2><a name="simple_locking_synopsis">Header
  184. <a href="../../../../boost/flyweight/simple_locking.hpp"><code>"boost/flyweight/simple_locking.hpp"</code></a> synopsis</a></h2>
  185. <h3><a name="simple_locking">Class <code>simple_locking</code></a></h3>
  186. <p>
  187. <a href="#locking"><code>Locking Policy</code></a> that specifies a basic
  188. mutex type based on the simplest synchronization mechanisms provided by
  189. the environment; When no threading capabilities are available,
  190. <code>simple_locking</code> specifies a dummy type without actual
  191. synchronization capabilities.
  192. </p>
  193. <h2><a name="no_locking_fwd_synopsis">Header
  194. <a href="../../../../boost/flyweight/no_locking_fwd.hpp"><code>"boost/flyweight/no_locking_fwd.hpp"</code></a> synopsis</a></h2>
  195. <blockquote><pre>
  196. <span class=keyword>namespace</span> <span class=identifier>boost</span><span class=special>{</span>
  197. <span class=keyword>namespace</span> <span class=identifier>flyweights</span><span class=special>{</span>
  198. <span class=keyword>struct</span> <span class=identifier>no_locking</span><span class=special>;</span>
  199. <span class=special>}</span> <span class=comment>// namespace boost::flyweights</span>
  200. <span class=special>}</span> <span class=comment>// namespace boost</span>
  201. </pre></blockquote>
  202. <p>
  203. <code>no_locking_fwd.hpp</code> forward declares the class
  204. <a href="#no_locking"><code>no_locking</code></a>.
  205. </p>
  206. <h2><a name="no_locking_synopsis">Header
  207. <a href="../../../../boost/flyweight/no_locking.hpp"><code>"boost/flyweight/no_locking.hpp"</code></a> synopsis</a></h2>
  208. <h3><a name="no_locking">Class <code>no_locking</code></a></h3>
  209. <p>
  210. Null <a href="#locking"><code>Locking Policy</code></a>: it specifies a dummy
  211. type that satisfies the formal requirements for the
  212. <a href="#preliminary"><code>Mutex</code></a> concept but does not perform
  213. thread blocking. <code>no_locking</code> should only be used in single-threaded
  214. environments.
  215. </p>
  216. <hr>
  217. <div class="prev_link"><a href="holders.html"><img src="../prev.gif" alt="holders" border="0"><br>
  218. Holders
  219. </a></div>
  220. <div class="up_link"><a href="index.html"><img src="../up.gif" alt="Boost.Flyweight reference" border="0"><br>
  221. Boost.Flyweight reference
  222. </a></div>
  223. <div class="next_link"><a href="tracking.html"><img src="../next.gif" alt="tracking policies" border="0"><br>
  224. Tracking policies
  225. </a></div><br clear="all" style="clear: all;">
  226. <br>
  227. <p>Revised March 9th 2010</p>
  228. <p>&copy; Copyright 2006-2010 Joaqu&iacute;n M L&oacute;pez Mu&ntilde;oz.
  229. Distributed under the Boost Software
  230. License, Version 1.0. (See accompanying file <a href="../../../../LICENSE_1_0.txt">
  231. LICENSE_1_0.txt</a> or copy at <a href="http://www.boost.org/LICENSE_1_0.txt">
  232. http://www.boost.org/LICENSE_1_0.txt</a>)
  233. </p>
  234. </body>
  235. </html>