examples.qbk 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. [/
  2. / Copyright (c) 2009 Helge Bahmann
  3. /
  4. / Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. /]
  7. [section:example_reference_counters Reference counting]
  8. The purpose of a ['reference counter] is to count the number
  9. of pointers to an object. The object can be destroyed as
  10. soon as the reference counter reaches zero.
  11. [section Implementation]
  12. [c++]
  13. #include <boost/intrusive_ptr.hpp>
  14. #include <boost/atomic.hpp>
  15. class X {
  16. public:
  17. typedef boost::intrusive_ptr<X> pointer;
  18. X() : refcount_(0) {}
  19. private:
  20. mutable boost::atomic<int> refcount_;
  21. friend void intrusive_ptr_add_ref(const X * x)
  22. {
  23. x->refcount_.fetch_add(1, boost::memory_order_relaxed);
  24. }
  25. friend void intrusive_ptr_release(const X * x)
  26. {
  27. if (x->refcount_.fetch_sub(1, boost::memory_order_release) == 1) {
  28. boost::atomic_thread_fence(boost::memory_order_acquire);
  29. delete x;
  30. }
  31. }
  32. };
  33. [endsect]
  34. [section Usage]
  35. [c++]
  36. X::pointer x = new X;
  37. [endsect]
  38. [section Discussion]
  39. Increasing the reference counter can always be done with
  40. [^memory_order_relaxed]: New references to an object can only
  41. be formed from an existing reference, and passing an existing
  42. reference from one thread to another must already provide any
  43. required synchronization.
  44. It is important to enforce any possible access to the object in
  45. one thread (through an existing reference) to ['happen before]
  46. deleting the object in a different thread. This is achieved
  47. by a "release" operation after dropping a reference (any
  48. access to the object through this reference must obviously
  49. happened before), and an "acquire" operation before
  50. deleting the object.
  51. It would be possible to use [^memory_order_acq_rel] for the
  52. [^fetch_sub] operation, but this results in unneeded "acquire"
  53. operations when the reference counter does not yet reach zero
  54. and may impose a performance penalty.
  55. [endsect]
  56. [endsect]
  57. [section:example_spinlock Spinlock]
  58. The purpose of a ['spin lock] is to prevent multiple threads
  59. from concurrently accessing a shared data structure. In contrast
  60. to a mutex, threads will busy-wait and waste CPU cycles instead
  61. of yielding the CPU to another thread. ['Do not use spinlocks
  62. unless you are certain that you understand the consequences.]
  63. [section Implementation]
  64. [c++]
  65. #include <boost/atomic.hpp>
  66. class spinlock {
  67. private:
  68. typedef enum {Locked, Unlocked} LockState;
  69. boost::atomic<LockState> state_;
  70. public:
  71. spinlock() : state_(Unlocked) {}
  72. void lock()
  73. {
  74. while (state_.exchange(Locked, boost::memory_order_acquire) == Locked) {
  75. /* busy-wait */
  76. }
  77. }
  78. void unlock()
  79. {
  80. state_.store(Unlocked, boost::memory_order_release);
  81. }
  82. };
  83. [endsect]
  84. [section Usage]
  85. [c++]
  86. spinlock s;
  87. s.lock();
  88. // access data structure here
  89. s.unlock();
  90. [endsect]
  91. [section Discussion]
  92. The purpose of the spinlock is to make sure that one access
  93. to the shared data structure always strictly "happens before"
  94. another. The usage of acquire/release in lock/unlock is required
  95. and sufficient to guarantee this ordering.
  96. It would be correct to write the "lock" operation in the following
  97. way:
  98. [c++]
  99. lock()
  100. {
  101. while (state_.exchange(Locked, boost::memory_order_relaxed) == Locked) {
  102. /* busy-wait */
  103. }
  104. atomic_thread_fence(boost::memory_order_acquire);
  105. }
  106. This "optimization" is however a) useless and b) may in fact hurt:
  107. a) Since the thread will be busily spinning on a blocked spinlock,
  108. it does not matter if it will waste the CPU cycles with just
  109. "exchange" operations or with both useless "exchange" and "acquire"
  110. operations. b) A tight "exchange" loop without any
  111. memory-synchronizing instruction introduced through an "acquire"
  112. operation will on some systems monopolize the memory subsystem
  113. and degrade the performance of other system components.
  114. [endsect]
  115. [endsect]
  116. [section:singleton Singleton with double-checked locking pattern]
  117. The purpose of the ['Singleton with double-checked locking pattern] is to ensure
  118. that at most one instance of a particular object is created.
  119. If one instance has been created already, access to the existing
  120. object should be as light-weight as possible.
  121. [section Implementation]
  122. [c++]
  123. #include <boost/atomic.hpp>
  124. #include <boost/thread/mutex.hpp>
  125. class X {
  126. public:
  127. static X * instance()
  128. {
  129. X * tmp = instance_.load(boost::memory_order_consume);
  130. if (!tmp) {
  131. boost::mutex::scoped_lock guard(instantiation_mutex);
  132. tmp = instance_.load(boost::memory_order_consume);
  133. if (!tmp) {
  134. tmp = new X;
  135. instance_.store(tmp, boost::memory_order_release);
  136. }
  137. }
  138. return tmp;
  139. }
  140. private:
  141. static boost::atomic<X *> instance_;
  142. static boost::mutex instantiation_mutex;
  143. };
  144. boost::atomic<X *> X::instance_(0);
  145. [endsect]
  146. [section Usage]
  147. [c++]
  148. X * x = X::instance();
  149. // dereference x
  150. [endsect]
  151. [section Discussion]
  152. The mutex makes sure that only one instance of the object is
  153. ever created. The [^instance] method must make sure that any
  154. dereference of the object strictly "happens after" creating
  155. the instance in another thread. The use of [^memory_order_release]
  156. after creating and initializing the object and [^memory_order_consume]
  157. before dereferencing the object provides this guarantee.
  158. It would be permissible to use [^memory_order_acquire] instead of
  159. [^memory_order_consume], but this provides a stronger guarantee
  160. than is required since only operations depending on the value of
  161. the pointer need to be ordered.
  162. [endsect]
  163. [endsect]
  164. [section:example_ringbuffer Wait-free ring buffer]
  165. A ['wait-free ring buffer] provides a mechanism for relaying objects
  166. from one single "producer" thread to one single "consumer" thread without
  167. any locks. The operations on this data structure are "wait-free" which
  168. means that each operation finishes within a constant number of steps.
  169. This makes this data structure suitable for use in hard real-time systems
  170. or for communication with interrupt/signal handlers.
  171. [section Implementation]
  172. [c++]
  173. #include <boost/atomic.hpp>
  174. template<typename T, size_t Size>
  175. class ringbuffer {
  176. public:
  177. ringbuffer() : head_(0), tail_(0) {}
  178. bool push(const T & value)
  179. {
  180. size_t head = head_.load(boost::memory_order_relaxed);
  181. size_t next_head = next(head);
  182. if (next_head == tail_.load(boost::memory_order_acquire))
  183. return false;
  184. ring_[head] = value;
  185. head_.store(next_head, boost::memory_order_release);
  186. return true;
  187. }
  188. bool pop(T & value)
  189. {
  190. size_t tail = tail_.load(boost::memory_order_relaxed);
  191. if (tail == head_.load(boost::memory_order_acquire))
  192. return false;
  193. value = ring_[tail];
  194. tail_.store(next(tail), boost::memory_order_release);
  195. return true;
  196. }
  197. private:
  198. size_t next(size_t current)
  199. {
  200. return (current + 1) % Size;
  201. }
  202. T ring_[Size];
  203. boost::atomic<size_t> head_, tail_;
  204. };
  205. [endsect]
  206. [section Usage]
  207. [c++]
  208. ringbuffer<int, 32> r;
  209. // try to insert an element
  210. if (r.push(42)) { /* succeeded */ }
  211. else { /* buffer full */ }
  212. // try to retrieve an element
  213. int value;
  214. if (r.pop(value)) { /* succeeded */ }
  215. else { /* buffer empty */ }
  216. [endsect]
  217. [section Discussion]
  218. The implementation makes sure that the ring indices do
  219. not "lap-around" each other to ensure that no elements
  220. are either lost or read twice.
  221. Furthermore it must guarantee that read-access to a
  222. particular object in [^pop] "happens after" it has been
  223. written in [^push]. This is achieved by writing [^head_ ]
  224. with "release" and reading it with "acquire". Conversely
  225. the implementation also ensures that read access to
  226. a particular ring element "happens before" before
  227. rewriting this element with a new value by accessing [^tail_]
  228. with appropriate ordering constraints.
  229. [endsect]
  230. [endsect]
  231. [section:mp_queue Wait-free multi-producer queue]
  232. The purpose of the ['wait-free multi-producer queue] is to allow
  233. an arbitrary number of producers to enqueue objects which are
  234. retrieved and processed in FIFO order by a single consumer.
  235. [section Implementation]
  236. [c++]
  237. template<typename T>
  238. class waitfree_queue {
  239. public:
  240. struct node {
  241. T data;
  242. node * next;
  243. };
  244. void push(const T &data)
  245. {
  246. node * n = new node;
  247. n->data = data;
  248. node * stale_head = head_.load(boost::memory_order_relaxed);
  249. do {
  250. n->next = stale_head;
  251. } while (!head_.compare_exchange_weak(stale_head, n, boost::memory_order_release));
  252. }
  253. node * pop_all(void)
  254. {
  255. T * last = pop_all_reverse(), * first = 0;
  256. while(last) {
  257. T * tmp = last;
  258. last = last->next;
  259. tmp->next = first;
  260. first = tmp;
  261. }
  262. return first;
  263. }
  264. waitfree_queue() : head_(0) {}
  265. // alternative interface if ordering is of no importance
  266. node * pop_all_reverse(void)
  267. {
  268. return head_.exchange(0, boost::memory_order_consume);
  269. }
  270. private:
  271. boost::atomic<node *> head_;
  272. };
  273. [endsect]
  274. [section Usage]
  275. [c++]
  276. waitfree_queue<int> q;
  277. // insert elements
  278. q.push(42);
  279. q.push(2);
  280. // pop elements
  281. waitfree_queue<int>::node * x = q.pop_all()
  282. while(x) {
  283. X * tmp = x;
  284. x = x->next;
  285. // process tmp->data, probably delete it afterwards
  286. delete tmp;
  287. }
  288. [endsect]
  289. [section Discussion]
  290. The implementation guarantees that all objects enqueued are
  291. processed in the order they were enqueued by building a singly-linked
  292. list of object in reverse processing order. The queue is atomically
  293. emptied by the consumer and brought into correct order.
  294. It must be guaranteed that any access to an object to be enqueued
  295. by the producer "happens before" any access by the consumer. This
  296. is assured by inserting objects into the list with ['release] and
  297. dequeuing them with ['consume] memory order. It is not
  298. necessary to use ['acquire] memory order in [^waitfree_queue::pop_all]
  299. because all operations involved depend on the value of
  300. the atomic pointer through dereference
  301. [endsect]
  302. [endsect]