sync_queues_ref.qbk 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  1. [/
  2. / Copyright (c) 2013 Vicente J. Botet Escriba
  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. [/////////////////////////////////////]
  8. [section:synchronized_queues Synchronized Queues -- EXPERIMENTAL]
  9. [warning These features are experimental and subject to change in future versions. There are not too much tests yet, so it is possible that you can find out some trivial bugs :(]
  10. [note These features are based on the [@http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2013/n3533.html [*N3533 - C++ Concurrent Queues]] C++1y proposal from Lawrence Crowl and Chris Mysen and [@http://www.manning.com/williams/ [*C++ Concurrency in Action]] from Anthony Williams.]
  11. [////////////////////]
  12. [section Introduction]
  13. Queues provide a mechanism for communicating data between components of a system.
  14. The existing deque in the standard library is an inherently sequential data structure. Its reference-returning element access operations cannot synchronize access to those elements with other queue operations. So, concurrent pushes and pops on queues require a different interface to the queue structure.
  15. Moreover, concurrency adds a new dimension for performance and semantics. Different queue implementation must trade off uncontended operation cost, contended operation cost, and element order guarantees. Some of these trade-offs will necessarily result in semantics weaker than a serial queue.
  16. [endsect]
  17. [/////////////////////////////////////]
  18. [section:tutorial Tutorial]
  19. Concurrent queues are a well know mechanism for communicating data between different threads.
  20. Concurrent queues have inherently copy/move semantics for the data handling operation. Reference-returning interfaces are forbidden as multiple access to these references can not be thread-safe.
  21. [endsect]
  22. [////////////////]
  23. [section Examples]
  24. [endsect]
  25. [/////////////////////////////////////]
  26. [section:ref Reference]
  27. [section:sync_queue_req Synchronized Queue Model]
  28. [section:bounded_unbounded Bounded-Unbounded Queues]
  29. One of the major features of a concurrent queue is whether it has a bounded-unbounded capacity.
  30. [endsect]
  31. [section:locking Locking/Lock-free Queues]
  32. Locking queues can by nature block waiting for the queue to be non-empty or non-full.
  33. Lock-free queues will have some trouble waiting for the queue to be non-empty or non-full queues. These queues can not define operations such as push (and pull for bounded queues). That is, it could have blocking operations (presumably emulated with busy wait) but not waiting operations.
  34. [endsect]
  35. [/////////////////////////////////////]
  36. [section:closed Closed Queue]
  37. Threads using a queue for communication need some mechanism to signal when the queue is no longer needed. The usual approach is add an additional out-of-band signal. However, this approach suffers from the flaw that threads waiting on either full or empty queues need to be woken up when the queue is no longer needed. Rather than require an out-of-band signal, we chose to directly support such a signal in the queue itself, which considerably simplifies coding.
  38. To achieve this signal, a thread may close a queue. Once closed, no new elements may be pushed onto the queue. Push operations on a closed queue will either return queue_op_status::closed (when they have a queue_op_status return type), set the closed parameter if it has one or throw sync_queue::closed (when they do not). Elements already on the queue may be pulled off. When a queue is empty and closed, pull operations will either return queue_op_status::closed (when they have a status return), set the closed parameter if it has one or throw sync_queue::closed (when they do not).
  39. [endsect]
  40. [/////////////////////////////////////]
  41. [section:exception Concurrent Queues Throw specification]
  42. [section:locking Locking]
  43. All the functions are defined as if we had in addition to its specific Throw specification the following:
  44. [variablelist
  45. [[Throws:] [Any exception thrown by the internal locking.]]
  46. ]
  47. [endsect]
  48. [/////////////////////////////////////]
  49. [section:bad_alloc Allocation]
  50. All the functions that allocate a resource are defined as if we had in addition to its specific Throw specification the following:
  51. [variablelist
  52. [[Throws:] [Any exception due to allocation errors.]]
  53. ]
  54. [endsect]
  55. [endsect]
  56. [/////////////////////////////////////]
  57. [section:BasicConcurrentQueue Basic Concurrent Queue Operations]
  58. The essential solution to the problem of concurrent queuing is to shift to value-based operations, rather than reference-based operations.
  59. The BasicConcurrentQueue concept models the basic operations of a concurrent queue.
  60. A type `Q` meets the BasicConcurrentQueue requirements if the following expressions are well-formed and have the specified semantics
  61. * Q::value_type
  62. * Q::size_type
  63. * `q.push_back(e);`
  64. * `q.push_back(rve);`
  65. * `q.pull_front(lre);`
  66. * `lre = q.pull_front();`
  67. [/* `spe = q.ptr_pull_front();`]
  68. * `b = q.empty();`
  69. * `u = q.size();`
  70. where
  71. * `q` denotes a value of type `Q`,
  72. * `e` denotes a value of type Q::value_type,
  73. * `u` denotes a value of type Q::size_type,
  74. * `lve` denotes an lvalue reference of type Q::value_type,
  75. * `rve` denotes an rvalue reference of type Q::value_type:
  76. [/* `spe` denotes a shared_ptr<Q::value_type>]
  77. * `qs` denotes a variable of of type `queue_op_status`,
  78. [/////////////////////////////////////]
  79. [section:push_back `q.push_back(e);`]
  80. [variablelist
  81. [[Effects:] [Waits until the queue is not full (for bounded queues) and then push back `e` to the queue copying it (this could need an allocation for unbounded queues).]]
  82. [[Synchronization:] [Prior pull-like operations on the same object synchronizes with this operation.]]
  83. [[Postcondition:] [`! q.empty()`.]]
  84. [[Return type:] [`void`.]]
  85. [[Throws:] [If the queue was closed, throws sync_queue_is_closed. Any exception thrown by the copy of `e`.]]
  86. [[Exception safety:] [If an exception is thrown then the queue state is unmodified.]]
  87. ]
  88. [endsect]
  89. [/////////////////////////////////////]
  90. [section:push_back_m `q.push_back(rve);`]
  91. [variablelist
  92. [[Effects:] [Waits until the queue is not full (for bounded queues) and then push `e` to the queue moving it back in the queue (this could need an allocation for unbounded queues).]]
  93. [[Synchronization:] [Prior pull-like operations on the same object synchronizes with this operation.]]
  94. [[Postcondition:] [`! q.empty()`.]]
  95. [[Return type:] [`void`.]]
  96. [[Throws:] [If the queue is closed, throws sync_queue_is_closed. Any exception thrown by the copy of `e`.]]
  97. [[Exception safety:] [If an exception is thrown then the queue state is unmodified.]]
  98. ]
  99. [endsect]
  100. [/////////////////////////////////////]
  101. [section:pull_front_lv `q.pull_front(lve)`]
  102. [variablelist
  103. [[Effects:] [Waits until the queue is not empty and then pull_front the element from the queue `q` and moves the pulled element into `lve` (this could need an allocation for unbounded queues).]]
  104. [[Synchronization:] [Prior pull-like operations on the same object synchronizes with this operation.]]
  105. [[Postcondition:] [`! q.full()`.]]
  106. [[Return type:] [`void`.]]
  107. [[Throws:] [Any exception thrown by the move of `e`.]]
  108. [[Exception safety:] [If an exception is thrown then the queue state is unmodified.]]
  109. ]
  110. [endsect]
  111. [/////////////////////////////////////]
  112. [section:pull_front `e = q.pull_front()`]
  113. [variablelist
  114. [[Requires:] [Q::value_type is no throw move constructible. This is needed to ensure the exception safety.]]
  115. [[Effects:] [Waits until the queue is not empty and not closed. If the queue is empty and closed throws sync_queue_is_closed. Otherwise pull the element from the queue `q` and moves the pulled element.]]
  116. [[Synchronization:] [Prior pull-like operations on the same object synchronizes with this operation.]]
  117. [[Postcondition:] [`! q.full()`.]]
  118. [[Return type:] [`Q::value_type`.]]
  119. [[Return:] [The pulled element.]]
  120. [[Throws:] [Any exception thrown by the copy of `e`.]]
  121. [[Exception safety:] [If an exception is thrown then the queue state is unmodified.]]
  122. ]
  123. [endsect]
  124. [/
  125. [/////////////////////////////////////]
  126. [section:ptr_pull_front `spe = q.ptr_pull_front()`]
  127. [variablelist
  128. [/[Requires:] [Q::value_type is no throw move assignable. This is needed to ensure the exception safety. ]]
  129. [[Effects:] [Waits until the queue is not empty and not closed. If the queue is empty and closed throws sync_queue_is_closed. Otherwise pull the element from the queue `q` and moves the pulled element into a shared_ptr.]]
  130. [[Synchronization:] [Prior pull-like operations on the same object synchronizes with this operation.]]
  131. [[Postcondition:] [`! q.full()`.]]
  132. [[Return type:] [`Q::value_type`.]]
  133. [[Return:] [A shared_ptr containing the pulled element.]]
  134. [[Throws:] [Any exception thrown by the move of `e`. Any exception throw when allocation resources are missing. ]]
  135. [[Exception safety:] [If an exception is thrown then the queue state is unmodified.]]
  136. ]
  137. [endsect]
  138. ]
  139. [endsect]
  140. [/////////////////////////////////////]
  141. [section:non_waiting Non-waiting Concurrent Queue Operations]
  142. The ConcurrentQueue concept models a queue with Non-waiting operations.
  143. A type `Q` meets the ConcurrentQueue requirements if is a model of a BasicConcurrentQueue and the following expressions are well-formed and have the specified semantics
  144. * `s = q.try_push_back(e);`
  145. * `s = q.try_push_back(rve);`
  146. * `s = q.try_pull_front(lre);`
  147. where
  148. * `q` denotes a value of type `Q`,
  149. * `e` denotes a value of type `Q::value_type`,
  150. * `s` denotes a value of type `queue_status`,
  151. * `u` denotes a value of type `Q::size_type`,
  152. * `lve` denotes an lvalue reference of type Q::value_type,
  153. * `rve` denotes an rvalue reference of type Q::value_type:
  154. [/* `spe` denotes a shared_ptr<Q::value_type>]
  155. [/////////////////////////////////////]
  156. [section:try_push_back `s = q.try_push_back(e);`]
  157. [variablelist
  158. [[Effects:] [If the queue `q` is not full and not closed, push back the `e` to the queue copying it.]]
  159. [[Synchronization:] [Prior pull-like operations on the same object synchronizes with this operation when the operation succeeds. ]]
  160. [[Return type:] [`queue_op_status`.]]
  161. [[Return:] [
  162. - If the queue is closed, returns `queue_op_status::closed`,
  163. - otherwise if the queue `q` is full return `queue_op_status::full`,
  164. - otherwise return `queue_op_status::success`;
  165. ]]
  166. [[Postcondition:] [If the call returns `queue_op_status::success`, `! q.empty()`.]]
  167. [[Throws:] [If the queue is closed, throws sync_queue_is_closed. Any exception thrown by the copy of `e`.]]
  168. [[Exception safety:] [If an exception is thrown then the queue state is unmodified.]]
  169. ]
  170. [endsect]
  171. [/////////////////////////////////////]
  172. [section:try_push_back_m `s = q.try_push_back(rve);`]
  173. [variablelist
  174. [[Effects:] [If the queue `q` is not full and not closed, push back the `e` onto the queue moving it.]]
  175. [[Synchronization:] [Prior pull-like operations on the same object synchronizes with this operation.]]
  176. [[Return type:] [`queue_op_status`.]]
  177. [[Return:] [
  178. - If the queue is closed, returns `queue_op_status::closed`,
  179. - otherwise if the queue `q` is full return `queue_op_status::full`,
  180. - otherwise return `queue_op_status::success`;
  181. ]]
  182. [[Postcondition:] [If the call returns `queue_op_status::success`, `! q.empty()`.]]
  183. [[Throws:] [ Any exception thrown by the copy of `e`.]]
  184. [[Exception safety:] [If an exception is thrown then the queue state is unmodified.]]
  185. ]
  186. [endsect]
  187. [/////////////////////////////////////]
  188. [section:try_pull_front_lv `s = q.try_pull_front(lve)`]
  189. [variablelist
  190. [[Effects:] [If the queue is not empty pulls the element from the queue `q` and moves the pulled element into `lve` (this could need an allocation for unbounded queues).]]
  191. [[Synchronization:] [Prior pull-like operations on the same object synchronizes with this operation.]]
  192. [[Postcondition:] [`! q.full()`.]]
  193. [[Return type:] [`bool`.]]
  194. [[Return:] [
  195. - If the queue `q` is empty return `queue_op_status::empty`,
  196. - otherwise return `queue_op_status::success`;
  197. ]]
  198. [[Throws:] [Any exception thrown by the move of `e`.]]
  199. [[Exception safety:] [If an exception is thrown then the queue state is unmodified.]]
  200. ]
  201. [endsect]
  202. [endsect]
  203. [/////////////////////////////////////]
  204. [section:non_blocking Non-blocking Concurrent Queue Operations]
  205. For cases when blocking for mutual exclusion is undesirable, we have non-blocking operations.
  206. The interface is the same as the try operations but is allowed to also return queue_op_status::busy
  207. in case the operation is unable to complete without blocking.
  208. Non-blocking operations are provided only for lock based queues
  209. * `s = q.nonblocking_push_back(nb, e);`
  210. * `s = q.nonblocking_push_back(nb, rve);`
  211. * `s = q.nonblocking_pull_front(nb, lre);`
  212. where
  213. * `q` denotes a value of type `Q`,
  214. * `e` denotes a value of type Q::value_type,
  215. * `s` denotes a value of type `queue_status`,
  216. * `lve` denotes an lvalue reference of type Q::value_type,
  217. * `rve` denotes an rvalue reference of type Q::value_type:
  218. [/* `spe` denotes a shared_ptr<Q::value_type>]
  219. [/////////////////////////////////////]
  220. [section:nonblocking_push_back `s = q.nonblocking_push_back(e);`]
  221. [variablelist
  222. [[Effects:] [If the queue `q` is not full and not closed, push back the `e` to the queue copying it.]]
  223. [[Synchronization:] [Prior pull-like operations on the same object synchronizes with this operation when the operation succeeds. ]]
  224. [[Return type:] [`queue_op_status`.]]
  225. [[Return:] [
  226. - If the operation would block, return queue_op_status::busy,
  227. - otherwise, if the queue is closed, return `queue_op_status::closed`,
  228. - otherwise, if the queue `q` is full return `queue_op_status::full`,
  229. - otherwise return `queue_op_status::success`;]]
  230. [[Postcondition:] [If the call returns `queue_op_status::success`, `! q.empty()`.]]
  231. [[Throws:] [If the queue is closed, throws sync_queue_is_closed. Any exception thrown by the copy of `e`.]]
  232. [[Exception safety:] [If an exception is thrown then the queue state is unmodified.]]
  233. ]
  234. [endsect]
  235. [/////////////////////////////////////]
  236. [section:nonblocking_push_back_m `s = q.nonblocking_push_back(rve);`]
  237. [variablelist
  238. [[Effects:] [If the queue `q` is not full and not closed, push back the `e` onto the queue moving it.]]
  239. [[Synchronization:] [Prior pull-like operations on the same object synchronizes with this operation.]]
  240. [[Return type:] [`queue_op_status`.]]
  241. [[Return:] [
  242. - If the operation would block, return queue_op_status::busy,
  243. - otherwise if the queue is closed, returns `queue_op_status::closed`,
  244. - otherwise if the queue `q` is full return `queue_op_status::full`,
  245. - otherwise return `queue_op_status::success`;]]
  246. [[Postcondition:] [If the call returns `queue_op_status::success`, `! q.empty()`.]]
  247. [[Throws:] [ Any exception thrown by the copy of `e`.]]
  248. [[Exception safety:] [If an exception is thrown then the queue state is unmodified.]]
  249. ]
  250. [endsect]
  251. [/////////////////////////////////////]
  252. [section:nonblocking_pull_front_lv `s = q.nonblocking_pull_front(lve)`]
  253. [variablelist
  254. [[Effects:] [If the queue is not empty pulls the element from the queue `q` and moves the pulled element into `lve` (this could need an allocation for unbounded queues).]]
  255. [[Synchronization:] [Prior pull-like operations on the same object synchronizes with this operation.]]
  256. [[Postcondition:] [`! q.full()`.]]
  257. [[Return type:] [`bool`.]]
  258. [[Return:] [
  259. - If the operation would block, return queue_op_status::busy,
  260. - otherwise if the queue `q` is empty return `queue_op_status::empty`,
  261. - otherwise return `queue_op_status::success`;]]
  262. [[Throws:] [Any exception thrown by the move of `e`.]]
  263. [[Exception safety:] [If an exception is thrown then the queue state is unmodified.]]
  264. ]
  265. [endsect]
  266. [endsect]
  267. [/////////////////////////////////////]
  268. [section:bounded Bounded Concurrent Queue Operations]
  269. Bounded queues add the following valid expressions
  270. * `Q q(u);`
  271. * `b = q.full();`
  272. * `u = q.capacity();`
  273. where
  274. * `q` denotes a value of type `Q`,
  275. * `b` denotes a value of type `bool`,
  276. * `u` denotes a value of type `Q::size_type`,
  277. [/////////////////////////////////////]
  278. [section:full `b = q.full();`]
  279. [variablelist
  280. [[Return type:] [`bool`.]]
  281. [[Return:] [Return `true` iff the queue is full.]]
  282. [[Remark:] [Not all queues will have a full state, and these would always return false if the function is provided.]]
  283. ]
  284. [endsect]
  285. [/////////////////////////////////////]
  286. [section:capacity `b = q.capacity();`]
  287. [variablelist
  288. [[Return type:] [`Q::size_type`.]]
  289. [[Return:] [Return the capacity of queue.]]
  290. ]
  291. [endsect]
  292. [endsect]
  293. [/////////////////////////////////////]
  294. [section:closed_op Closed Concurrent Queue Operations]
  295. Closed queues add the following valid expressions
  296. * `q.close();`
  297. * `b = q.closed();`
  298. * `s = q.wait_push_back(e);`
  299. * `s = q.wait_push_back(rve);`
  300. * `s = q.wait_pull_front(lre);`
  301. [/////////////////////////////////////]
  302. [section:wait_push_back `q.close();`]
  303. [variablelist
  304. [[Effects:] [Close the queue.]]
  305. ]
  306. [endsect]
  307. [/////////////////////////////////////]
  308. [section:wait_push_back `b = q.closed();`]
  309. [variablelist
  310. [[Return type:] [`bool`.]]
  311. [[Return:] [Return `true` iff the queue is closed.]]
  312. ]
  313. [endsect]
  314. [/////////////////////////////////////]
  315. [section:wait_push_back `s = q.wait_push_back(e);`]
  316. [variablelist
  317. [[Effects:] [Waits until the queue is not full (for bounded queues) and then push back `e` to the queue copying it (this could need an allocation for unbounded queues).]]
  318. [[Synchronization:] [Prior pull-like operations on the same object synchronizes with this operation.]]
  319. [[Postcondition:] [`! q.empty()`.]]
  320. [[Return type:] [`queue_op_status`.]]
  321. [[Return:] [
  322. - If the queue is closed return `queue_op_status::closed`,
  323. - otherwise, return `queue_op_status::success` if no exception is thrown.
  324. ]]
  325. [[Throws:] [Any exception thrown by the copy of `e`.]]
  326. [[Exception safety:] [If an exception is thrown then the queue state is unmodified.]]
  327. ]
  328. [endsect]
  329. [/////////////////////////////////////]
  330. [section:wait_push_back_m `s = q.wait_push_back(rve);`]
  331. [variablelist
  332. [[Effects:] [Waits until the queue is not full (for bounded queues) and then push `e` to the queue moving it back in the queue (this could need an allocation for unbounded queues).]]
  333. [[Synchronization:] [Prior pull-like operations on the same object synchronizes with this operation.]]
  334. [[Postcondition:] [`! q.empty()`.]]
  335. [[Return type:] [`queue_op_status`.]]
  336. [[Return:] [
  337. - If the queue is closed return `queue_op_status::closed`,
  338. - otherwise, return `queue_op_status::success` if no exception is thrown.
  339. .]]
  340. [[Throws:] [Any exception thrown by the copy of `e`.]]
  341. [[Exception safety:] [If an exception is thrown then the queue state is unmodified.]]
  342. ]
  343. [endsect]
  344. [/////////////////////////////////////]
  345. [section:wait_pull_front_lv `s = q.wait_pull_front(lve)`]
  346. [variablelist
  347. [[Effects:] [if the queue is not empty and not closed, waits until the queue is not empty and then pull_front the element from the queue `q` and moves the pulled element into `lve`.]]
  348. [[Synchronization:] [Prior pull-like operations on the same object synchronizes with this operation.]]
  349. [[Postcondition:] [`! q.full()`.]]
  350. [[Return type:] [`queue_op_status`.]]
  351. [[Return:] [
  352. - If the queue is empty and closed, return `queue_op_status::closed`,
  353. - otherwise, return `queue_op_status::success` if no exception is thrown.
  354. ]]
  355. [[Throws:] [Any exception thrown by the move of `e`.]]
  356. [[Exception safety:] [If an exception is thrown then the queue state is unmodified.]]
  357. ]
  358. [endsect]
  359. [endsect]
  360. [endsect]
  361. [/////////////////////////////////////]
  362. [section:queue_op_status Queue Operation Status]
  363. #include <boost/thread/concurrent_queues/queue_op_status.hpp>
  364. namespace boost
  365. {
  366. enum class queue_op_status { success = 0, empty, full, closed, busy }
  367. }
  368. [endsect]
  369. [/////////////////////////////////////]
  370. [section:queue_base Queue Base]
  371. #include <boost/thread/concurrent_queues/queue_base.hpp>
  372. namespace boost
  373. {
  374. template <typename ValueType, class SizeType=std::size_t>
  375. class queue_base
  376. {
  377. public:
  378. typedef ValueType value_type;
  379. typedef SizeType size_type;
  380. // Constructors/Assignment/Destructors
  381. virtual ~queue_base() {};
  382. // Observers
  383. virtual bool empty() const = 0;
  384. virtual bool full() const = 0;
  385. virtual size_type size() const = 0;
  386. virtual bool closed() const = 0;
  387. // Modifiers
  388. virtual void close() = 0;
  389. virtual void push_back(const value_type& x) = 0;
  390. virtual void push_back(BOOST_THREAD_RV_REF(value_type) x) = 0;
  391. virtual void pull_front(value_type&) = 0;
  392. virtual value_type pull_front() = 0;
  393. virtual queue_op_status try_push_back(const value_type& x) = 0;
  394. virtual queue_op_status try_push_back(BOOST_THREAD_RV_REF(value_type) x) = 0;
  395. virtual queue_op_status try_pull_front(value_type&) = 0;
  396. virtual queue_op_status nonblocking_push_back(const value_type& x) = 0;
  397. virtual queue_op_status nonblocking_push_back(BOOST_THREAD_RV_REF(value_type) x) = 0;
  398. virtual queue_op_status nonblocking_pull_front(value_type&) = 0;
  399. virtual queue_op_status wait_push_back(const value_type& x) = 0;
  400. virtual queue_op_status wait_push_back(BOOST_THREAD_RV_REF(value_type) x) = 0;
  401. virtual queue_op_status wait_pull_front(value_type& elem) = 0;
  402. };
  403. }
  404. [endsect]
  405. [/////////////////////////////////////]
  406. [section:queue_adaptor Queue Adaptor]
  407. #include <boost/thread/concurrent_queues/queue_adaptor.hpp>
  408. namespace boost
  409. {
  410. template <typename Queue>
  411. class queue_adaptor : public queue_base<typename Queue::value_type, typename Queue::size_type>
  412. {
  413. public:
  414. typedef typename Queue::value_type value_type;
  415. typedef typename Queue::size_type size_type;
  416. // Constructors/Assignment/Destructors
  417. queue_adaptor();
  418. // Observers
  419. bool empty() const;
  420. bool full() const;
  421. size_type size() const { return queue.size(); }
  422. bool closed() const;
  423. // Modifiers
  424. void close();
  425. void push_back(const value_type& x);
  426. void push_back(BOOST_THREAD_RV_REF(value_type) x);
  427. void pull_front(value_type& x);
  428. value_type pull_front();
  429. queue_op_status try_push_back(const value_type& x);
  430. queue_op_status try_push_back(BOOST_THREAD_RV_REF(value_type) x);
  431. queue_op_status try_pull_front(value_type& x);
  432. queue_op_status nonblocking_push_back(const value_type& x);
  433. queue_op_status nonblocking_push_back(BOOST_THREAD_RV_REF(value_type) x);
  434. queue_op_status nonblocking_pull_front(value_type& x);
  435. queue_op_status wait_push_back(const value_type& x);
  436. queue_op_status wait_push_back(BOOST_THREAD_RV_REF(value_type) x);
  437. queue_op_status wait_pull_front(value_type& x);
  438. };
  439. }
  440. [endsect]
  441. [/////////////////////////////////////]
  442. [section:queue_views Queue Views]
  443. #include <boost/thread/concurrent_queues/queue_views.hpp>
  444. namespace boost
  445. {
  446. template <typename Queue>
  447. class queue_back_view;
  448. template <typename Queue>
  449. class queue_front_view
  450. template <class T>
  451. using queue_back = queue_back_view<queue_base<T>>;
  452. template <class T>
  453. using queue_front = queue_front_view<queue_base<T>>;
  454. }
  455. [/////////////////////////////////////]
  456. [section:queue_back_view Class template `queue_back_view<>`]
  457. template <typename Queue>
  458. class queue_back_view
  459. {
  460. public:
  461. typedef typename Queue::value_type value_type;
  462. typedef typename Queue::size_type size_type;
  463. // Constructors/Assignment/Destructors
  464. queue_back_view(Queue& q) noexcept;
  465. // Observers
  466. bool empty() const;
  467. bool full() const;
  468. size_type size() const;
  469. bool closed() const;
  470. // Modifiers
  471. void close();
  472. void push(const value_type& x);
  473. void push(BOOST_THREAD_RV_REF(value_type) x);
  474. queue_op_status try_push(const value_type& x);
  475. queue_op_status try_push(BOOST_THREAD_RV_REF(value_type) x);
  476. queue_op_status nonblocking_push(const value_type& x);
  477. queue_op_status nonblocking_push(BOOST_THREAD_RV_REF(value_type) x);
  478. queue_op_status wait_push(const value_type& x);
  479. queue_op_status wait_push(BOOST_THREAD_RV_REF(value_type) x);
  480. };
  481. [endsect]
  482. [/////////////////////////////////////]
  483. [section:queue_front_view Class template `queue_front_view<>`]
  484. template <typename Queue>
  485. class queue_front_view
  486. {
  487. public:
  488. typedef typename Queue::value_type value_type;
  489. typedef typename Queue::size_type size_type;
  490. // Constructors/Assignment/Destructors
  491. queue_front_view(Queue& q) BOOST_NOEXCEPT;
  492. // Observers
  493. bool empty() const;
  494. bool full() const;
  495. size_type size() const;
  496. bool closed() const;
  497. // Modifiers
  498. void close();
  499. void pull(value_type& x);
  500. value_type pull();
  501. queue_op_status try_pull(value_type& x);
  502. queue_op_status nonblocking_pull(value_type& x);
  503. queue_op_status wait_pull(value_type& x);
  504. };
  505. [endsect]
  506. [endsect]
  507. [/////////////////////////////////////]
  508. [section:sync_bounded_queue_ref Synchronized Bounded Queue]
  509. #include <boost/thread/sync_bounded_queue.hpp>
  510. namespace boost
  511. {
  512. struct sync_queue_is_closed : std::exception {};
  513. template <typename ValueType>
  514. class sync_bounded_queue;
  515. // Stream-like operators
  516. template <typename ValueType>
  517. sync_bounded_queue<ValueType>& operator<<(sync_bounded_queue<ValueType>& sbq, ValueType&& elem);
  518. template <typename ValueType>
  519. sync_bounded_queue<ValueType>& operator<<(sync_bounded_queue<ValueType>& sbq, ValueType const&elem);
  520. template <typename ValueType>
  521. sync_bounded_queue<ValueType>& operator>>(sync_bounded_queue<ValueType>& sbq, ValueType &elem);
  522. }
  523. [/////////////////////////////////////]
  524. [section:sync_queue_is_closed Class `sync_queue_is_closed`]
  525. #include <boost/thread/sync_bounded_queue.hpp>
  526. namespace boost
  527. {
  528. struct sync_queue_is_closed : std::exception {};
  529. }
  530. [endsect]
  531. [/////////////////////////////////////]
  532. [section:sync_bounded_queue Class template `sync_bounded_queue<>`]
  533. #include <boost/thread/sync_bounded_queue.hpp>
  534. namespace boost
  535. {
  536. template <typename ValueType>
  537. class sync_bounded_queue
  538. {
  539. public:
  540. typedef ValueType value_type;
  541. typedef std::size_t size_type;
  542. sync_bounded_queue(sync_bounded_queue const&) = delete;
  543. sync_bounded_queue& operator=(sync_bounded_queue const&) = delete;
  544. explicit sync_bounded_queue(size_type max_elems);
  545. template <typename Range>
  546. sync_bounded_queue(size_type max_elems, Range range);
  547. ~sync_bounded_queue();
  548. // Observers
  549. bool empty() const;
  550. bool full() const;
  551. size_type capacity() const;
  552. size_type size() const;
  553. bool closed() const;
  554. // Modifiers
  555. void push_back(const value_type& x);
  556. void push_back(value_type&& x);
  557. queue_op_status try_push_back(const value_type& x);
  558. queue_op_status try_push_back(value_type&&) x);
  559. queue_op_status nonblocking_push_back(const value_type& x);
  560. queue_op_status nonblocking_push_back(value_type&& x);
  561. void pull_front(value_type&);
  562. value_type pull_front();
  563. queue_op_status try_pull_front(value_type&);
  564. queue_op_status nonblocking_pull_front(value_type&);
  565. void close();
  566. };
  567. }
  568. [/ shared_ptr<ValueType> ptr_pull_front();]
  569. [/////////////////////////////////////]
  570. [section:constructor Constructor `sync_bounded_queue(size_type)`]
  571. explicit sync_bounded_queue(size_type max_elems);
  572. [variablelist
  573. [[Effects:] [Constructs a sync_bounded_queue with a maximum number of elements given by `max_elems`. ]]
  574. [[Throws:] [any exception that can be throw because of resources unavailable. ]]
  575. ]
  576. [endsect]
  577. [/////////////////////////////////////]
  578. [section:constructort Template Constructor `sync_bounded_queue(size_type, Range)`]
  579. template <typename Range>
  580. sync_bounded_queue(size_type max_elems, Range range);
  581. [variablelist
  582. [[Effects:] [Constructs a sync_bounded_queue with a maximum number of elements given by `max_elems` and push back the elements of the range. ]]
  583. [[Throws:] [any exception that can be throw because of resources unavailable. ]]
  584. ]
  585. [endsect]
  586. [endsect]
  587. [/////////////////////////////////////]
  588. [section:stream_out_operators Non-Member Function `operator<<()`]
  589. #include <boost/thread/sync_bounded_queue.hpp>
  590. namespace boost
  591. {
  592. template <typename ValueType>
  593. sync_bounded_queue<ValueType>& operator<<(sync_bounded_queue<ValueType>& sbq, ValueType&& elem);
  594. template <typename ValueType>
  595. sync_bounded_queue<ValueType>& operator<<(sync_bounded_queue<ValueType>& sbq, ValueType const&elem);
  596. }
  597. [endsect]
  598. [/////////////////////////////////////]
  599. [section:stream_in_operators Non-Member Function `operator>>()`]
  600. #include <boost/thread/sync_bounded_queue.hpp>
  601. namespace boost
  602. {
  603. template <typename ValueType>
  604. sync_bounded_queue<ValueType>& operator>>(sync_bounded_queue<ValueType>& sbq, ValueType &elem);
  605. }
  606. [endsect]
  607. [endsect]
  608. [/////////////////////////////////////]
  609. [section:sync_queue_ref Synchronized Unbounded Queue]
  610. #include <boost/thread/sync_queue.hpp>
  611. namespace boost
  612. {
  613. template <typename ValueType>
  614. class sync_queue;
  615. // Stream-like operators
  616. template <typename ValueType>
  617. sync_queue<ValueType>& operator<<(sync_queue<ValueType>& sbq, ValueType&& elem);
  618. template <typename ValueType>
  619. sync_queue<ValueType>& operator<<(sync_queue<ValueType>& sbq, ValueType const&elem);
  620. template <typename ValueType>
  621. sync_queue<ValueType>& operator>>(sync_queue<ValueType>& sbq, ValueType &elem);
  622. }
  623. [/////////////////////////////////////]
  624. [section:sync_queue Class template `sync_queue<>`]
  625. #include <boost/thread/sync_queue.hpp>
  626. namespace boost
  627. {
  628. template <typename ValueType, class Container = csbl::devector<ValueType>>
  629. class sync_queue
  630. {
  631. public:
  632. typedef ValueType value_type;
  633. typedef Container underlying_queue_type;
  634. typedef typename Container::size_type size_type;
  635. sync_queue(sync_queue const&) = delete;
  636. sync_queue& operator=(sync_queue const&) = delete;
  637. sync_queue();
  638. explicit template <typename Range>
  639. sync_queue(Range range); // Not yet implemented
  640. ~sync_queue();
  641. // Observers
  642. bool empty() const;
  643. bool full() const;
  644. size_type size() const;
  645. bool closed() const;
  646. // Modifiers
  647. void push_back(const value_type& x);
  648. void push_back(value_type&& x);
  649. queue_op_status try_push_back(const value_type& x);
  650. queue_op_status try_push_back(value_type&&) x);
  651. queue_op_status nonblocking_push_back(const value_type& x);
  652. queue_op_status nonblocking_push_back(value_type&& x);
  653. void pull_front(value_type&);
  654. value_type pull_front();
  655. queue_op_status try_pull_front(value_type&);
  656. queue_op_status nonblocking_pull_front(value_type&);
  657. underlying_queue_type underlying_queue() noexcept;
  658. void close();
  659. };
  660. }
  661. [/ shared_ptr<ValueType> ptr_pull_front();]
  662. [/////////////////////////////////////]
  663. [section:constructor Constructor `sync_queue(size_type)`]
  664. explicit sync_queue();
  665. [variablelist
  666. [[Effects:] [Constructs an empty sync_queue. ]]
  667. [[Throws:] [any exception that can be throw because of resources unavailable. ]]
  668. ]
  669. [endsect]
  670. [/////////////////////////////////////]
  671. [section:full Member Function `full()`]
  672. bool full() const;
  673. [variablelist
  674. [[Returns:] [false. ]]
  675. ]
  676. [endsect]
  677. [/////////////////////////////////////]
  678. [section:constructor Member Function `underlying_queue()`]
  679. underlying_queue_type underlying_queue() noexcept;
  680. [variablelist
  681. [[Returns:] [Moves internal queue. ]]
  682. ]
  683. [endsect]
  684. [endsect]
  685. [/////////////////////////////////////]
  686. [section:stream_out_operators Non-Member Function `operator<<()`]
  687. #include <boost/thread/sync_queue.hpp>
  688. namespace boost
  689. {
  690. template <typename ValueType>
  691. sync_queue<ValueType>& operator<<(sync_queue<ValueType>& sbq, ValueType&& elem);
  692. template <typename ValueType>
  693. sync_queue<ValueType>& operator<<(sync_queue<ValueType>& sbq, ValueType const&elem);
  694. }
  695. [endsect]
  696. [/////////////////////////////////////]
  697. [section:stream_in_operators Non-Member Function `operator>>()`]
  698. #include <boost/thread/sync_queue.hpp>
  699. namespace boost
  700. {
  701. template <typename ValueType>
  702. sync_queue<ValueType>& operator>>(sync_queue<ValueType>& sbq, ValueType &elem);
  703. }
  704. [endsect]
  705. [endsect]
  706. [endsect]
  707. [endsect]