multi_thread_pass.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. // Copyright (C) 2013 Vicente J. Botet Escriba
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. // <boost/thread/sync_bounded_queue.hpp>
  6. // class sync_queue<T>
  7. // push || pull;
  8. #include <boost/config.hpp>
  9. #if ! defined BOOST_NO_CXX11_DECLTYPE
  10. #define BOOST_RESULT_OF_USE_DECLTYPE
  11. #endif
  12. #define BOOST_THREAD_VERSION 4
  13. #include <boost/thread/sync_bounded_queue.hpp>
  14. #include <boost/thread/future.hpp>
  15. #include <boost/thread/barrier.hpp>
  16. #include <boost/detail/lightweight_test.hpp>
  17. struct call_push
  18. {
  19. boost::sync_bounded_queue<int> &q_;
  20. boost::barrier& go_;
  21. call_push(boost::sync_bounded_queue<int> &q, boost::barrier &go) :
  22. q_(q), go_(go)
  23. {
  24. }
  25. typedef void result_type;
  26. void operator()()
  27. {
  28. go_.count_down_and_wait();
  29. q_.push(42);
  30. }
  31. };
  32. struct call_push_2
  33. {
  34. boost::sync_bounded_queue<int> &q_;
  35. boost::barrier& go_;
  36. boost::barrier& end_;
  37. call_push_2(boost::sync_bounded_queue<int> &q, boost::barrier &go, boost::barrier &end) :
  38. q_(q), go_(go), end_(end)
  39. {
  40. }
  41. typedef void result_type;
  42. void operator()()
  43. {
  44. go_.count_down_and_wait();
  45. q_.push(42);
  46. end_.count_down_and_wait();
  47. }
  48. };
  49. struct call_pull
  50. {
  51. boost::sync_bounded_queue<int> &q_;
  52. boost::barrier& go_;
  53. call_pull(boost::sync_bounded_queue<int> &q, boost::barrier &go) :
  54. q_(q), go_(go)
  55. {
  56. }
  57. typedef int result_type;
  58. int operator()()
  59. {
  60. go_.count_down_and_wait();
  61. return q_.pull();
  62. }
  63. };
  64. void test_concurrent_push_and_pull_on_empty_queue()
  65. {
  66. boost::sync_bounded_queue<int> q(4);
  67. boost::barrier go(2);
  68. boost::future<void> push_done;
  69. boost::future<int> pull_done;
  70. try
  71. {
  72. push_done=boost::async(boost::launch::async,
  73. #if ! defined BOOST_NO_CXX11_LAMBDAS
  74. [&q,&go]()
  75. {
  76. go.wait();
  77. q.push(42);
  78. }
  79. #else
  80. call_push(q,go)
  81. #endif
  82. );
  83. pull_done=boost::async(boost::launch::async,
  84. #if ! defined BOOST_NO_CXX11_LAMBDAS
  85. [&q,&go]() -> int
  86. {
  87. go.wait();
  88. return q.pull();
  89. }
  90. #else
  91. call_pull(q,go)
  92. #endif
  93. );
  94. push_done.get();
  95. BOOST_TEST_EQ(pull_done.get(), 42);
  96. BOOST_TEST(q.empty());
  97. }
  98. catch (...)
  99. {
  100. BOOST_TEST(false);
  101. }
  102. }
  103. void test_concurrent_push_on_empty_queue()
  104. {
  105. boost::sync_bounded_queue<int> q(4);
  106. const unsigned int n = 3;
  107. boost::barrier go(n);
  108. boost::future<void> push_done[n];
  109. try
  110. {
  111. for (unsigned int i =0; i< n; ++i)
  112. push_done[i]=boost::async(boost::launch::async,
  113. #if ! defined BOOST_NO_CXX11_LAMBDAS
  114. [&q,&go]()
  115. {
  116. go.wait();
  117. q.push(42);
  118. }
  119. #else
  120. call_push(q,go)
  121. #endif
  122. );
  123. for (unsigned int i = 0; i < n; ++i)
  124. push_done[i].get();
  125. BOOST_TEST(!q.empty());
  126. for (unsigned int i =0; i< n; ++i)
  127. BOOST_TEST_EQ(q.pull(), 42);
  128. BOOST_TEST(q.empty());
  129. }
  130. catch (...)
  131. {
  132. BOOST_TEST(false);
  133. }
  134. }
  135. void test_concurrent_push_on_full_queue()
  136. {
  137. const unsigned int size = 2;
  138. boost::sync_bounded_queue<int> q(size);
  139. const unsigned int n = 2*size;
  140. boost::barrier go(n);
  141. boost::barrier end(size+1);
  142. boost::future<void> push_done[n];
  143. try
  144. {
  145. for (unsigned int i =0; i< n; ++i)
  146. push_done[i]=boost::async(boost::launch::async,
  147. #if ! defined BOOST_NO_CXX11_LAMBDAS
  148. [&q,&go,&end]()
  149. {
  150. go.wait();
  151. q.push(42);
  152. end.wait();
  153. }
  154. #else
  155. call_push_2(q,go,end)
  156. #endif
  157. );
  158. end.wait();
  159. BOOST_TEST(!q.empty());
  160. BOOST_TEST(q.full());
  161. for (unsigned int i =0; i< size; ++i)
  162. BOOST_TEST_EQ(q.pull(), 42);
  163. end.wait();
  164. for (unsigned int i = 0; i < n; ++i)
  165. push_done[i].get();
  166. BOOST_TEST(!q.empty());
  167. for (unsigned int i =0; i< size; ++i)
  168. BOOST_TEST_EQ(q.pull(), 42);
  169. BOOST_TEST(q.empty());
  170. }
  171. catch (...)
  172. {
  173. BOOST_TEST(false);
  174. }
  175. }
  176. void test_concurrent_pull_on_queue()
  177. {
  178. boost::sync_bounded_queue<int> q(4);
  179. const unsigned int n = 3;
  180. boost::barrier go(n);
  181. boost::future<int> pull_done[n];
  182. try
  183. {
  184. for (unsigned int i =0; i< n; ++i)
  185. q.push(42);
  186. for (unsigned int i =0; i< n; ++i)
  187. pull_done[i]=boost::async(boost::launch::async,
  188. #if ! defined BOOST_NO_CXX11_LAMBDAS
  189. [&q,&go]() -> int
  190. {
  191. go.wait();
  192. return q.pull();
  193. }
  194. #else
  195. call_pull(q,go)
  196. #endif
  197. );
  198. for (unsigned int i = 0; i < n; ++i)
  199. BOOST_TEST_EQ(pull_done[i].get(), 42);
  200. BOOST_TEST(q.empty());
  201. }
  202. catch (...)
  203. {
  204. BOOST_TEST(false);
  205. }
  206. }
  207. int main()
  208. {
  209. test_concurrent_push_and_pull_on_empty_queue();
  210. test_concurrent_push_on_empty_queue();
  211. test_concurrent_push_on_full_queue();
  212. test_concurrent_pull_on_queue();
  213. return boost::report_errors();
  214. }