task_region_pass.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. // Copyright (C) 2014-2015 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/experimental/parallel/v1/exception_list.hpp>
  6. #define BOOST_THREAD_VERSION 4
  7. #define BOOST_THREAD_PROVIDES_EXECUTORS
  8. #include <boost/config.hpp>
  9. #if ! defined BOOST_NO_CXX11_DECLTYPE
  10. #define BOOST_RESULT_OF_USE_DECLTYPE
  11. #endif
  12. #include <boost/thread/experimental/parallel/v2/task_region.hpp>
  13. #include <string>
  14. #include <boost/detail/lightweight_test.hpp>
  15. #if ! defined BOOST_NO_CXX11_LAMBDAS && defined(BOOST_THREAD_PROVIDES_INVOKE)
  16. using boost::experimental::parallel::v2::task_region;
  17. using boost::experimental::parallel::v2::task_region_handle;
  18. using boost::experimental::parallel::v1::exception_list;
  19. void run_no_exception()
  20. {
  21. std::string s("test");
  22. bool parent_flag = false;
  23. bool task1_flag = false;
  24. bool task2_flag = false;
  25. bool task21_flag = false;
  26. bool task3_flag = false;
  27. task_region([&](task_region_handle& trh)
  28. {
  29. parent_flag = true;
  30. trh.run([&]()
  31. {
  32. task1_flag = true;
  33. std::cout << "task1: " << s << std::endl;
  34. });
  35. trh.run([&]()
  36. {
  37. task2_flag = true;
  38. std::cout << "task2" << std::endl;
  39. task_region([&](task_region_handle& trh)
  40. {
  41. trh.run([&]()
  42. {
  43. task21_flag = true;
  44. std::cout << "task2.1" << std::endl;
  45. });
  46. });
  47. });
  48. int i = 0, j = 10, k = 20;
  49. trh.run([=, &task3_flag]()
  50. {
  51. task3_flag = true;
  52. std::cout << "task3: " << i << " " << j << " " << k << std::endl;
  53. });
  54. std::cout << "parent" << std::endl;
  55. });
  56. BOOST_TEST(parent_flag);
  57. BOOST_TEST(task1_flag);
  58. BOOST_TEST(task2_flag);
  59. BOOST_TEST(task21_flag);
  60. BOOST_TEST(task3_flag);
  61. }
  62. void run_no_exception_wait()
  63. {
  64. std::string s("test");
  65. bool parent_flag = false;
  66. bool wait_flag = false;
  67. bool task1_flag = false;
  68. bool task2_flag = false;
  69. bool task21_flag = false;
  70. bool task3_flag = false;
  71. task_region([&](task_region_handle& trh)
  72. {
  73. parent_flag = true;
  74. trh.run([&]()
  75. {
  76. task1_flag = true;
  77. std::cout << "task1: " << s << std::endl;
  78. });
  79. trh.run([&]()
  80. {
  81. task2_flag = true;
  82. std::cout << "task2" << std::endl;
  83. task_region([&](task_region_handle& trh)
  84. {
  85. trh.run([&]()
  86. {
  87. task21_flag = true;
  88. std::cout << "task2.1" << std::endl;
  89. });
  90. });
  91. });
  92. int i = 0, j = 10, k = 20;
  93. trh.run([=, &task3_flag]()
  94. {
  95. task3_flag = true;
  96. std::cout << "task3: " << i << " " << j << " " << k << std::endl;
  97. });
  98. std::cout << "before" << std::endl;
  99. trh.wait();
  100. wait_flag = true;
  101. std::cout << "parent" << std::endl;
  102. });
  103. BOOST_TEST(parent_flag);
  104. BOOST_TEST(wait_flag);
  105. BOOST_TEST(task1_flag);
  106. BOOST_TEST(task2_flag);
  107. BOOST_TEST(task21_flag);
  108. BOOST_TEST(task3_flag);
  109. }
  110. void run_exception_1()
  111. {
  112. try
  113. {
  114. task_region([](task_region_handle& trh)
  115. {
  116. trh.run([]()
  117. {
  118. std::cout << "task1" << std::endl;
  119. throw 1;
  120. });
  121. boost::this_thread::sleep_for(boost::chrono::seconds(1));
  122. trh.run([]()
  123. {
  124. std::cout << "task3" << std::endl;
  125. });
  126. #if defined BOOST_THREAD_TASK_REGION_HAS_SHARED_CANCELED
  127. BOOST_TEST(false);
  128. #endif
  129. });
  130. BOOST_TEST(false);
  131. }
  132. catch (exception_list const& e)
  133. {
  134. BOOST_TEST_EQ(e.size(), 1u);
  135. }
  136. catch (...)
  137. {
  138. BOOST_TEST(false);
  139. }
  140. }
  141. void run_exception()
  142. {
  143. try
  144. {
  145. task_region([](task_region_handle& trh)
  146. {
  147. trh.run([]()
  148. {
  149. std::cout << "task1" << std::endl;
  150. throw 1;
  151. });
  152. trh.run([]()
  153. {
  154. std::cout << "task2" << std::endl;
  155. throw 2;
  156. });
  157. boost::this_thread::sleep_for(boost::chrono::seconds(1));
  158. trh.run([]()
  159. {
  160. std::cout << "task3" << std::endl;
  161. throw 3;
  162. });
  163. std::cout << "parent" << std::endl;
  164. throw 100;
  165. });
  166. BOOST_TEST(false);
  167. }
  168. catch (exception_list const& el)
  169. {
  170. BOOST_TEST(el.size() >= 1u);
  171. for (boost::exception_ptr const& e: el)
  172. {
  173. try {
  174. boost::rethrow_exception(e);
  175. }
  176. catch (boost::exception&)
  177. {
  178. BOOST_TEST(true);
  179. }
  180. catch (int i) // this doesn't works yet
  181. {
  182. BOOST_TEST((i == 1) || (i == 2) || (i == 3));
  183. }
  184. catch (...)
  185. {
  186. BOOST_TEST(false);
  187. }
  188. }
  189. }
  190. catch (...)
  191. {
  192. BOOST_TEST(false);
  193. }
  194. }
  195. void run_nested_exception()
  196. {
  197. std::string s("test");
  198. bool parent_flag = false;
  199. bool task1_flag = false;
  200. bool task2_flag = false;
  201. bool task21_flag = false;
  202. bool task3_flag = false;
  203. try
  204. {
  205. task_region([&](task_region_handle& trh)
  206. {
  207. parent_flag = true;
  208. trh.run([&]()
  209. {
  210. task1_flag = true;
  211. std::cout << "task1: " << s << std::endl;
  212. });
  213. trh.run([&]()
  214. {
  215. task2_flag = true;
  216. std::cout << "task2" << std::endl;
  217. task_region([&](task_region_handle& trh)
  218. {
  219. trh.run([&]()
  220. {
  221. task21_flag = true;
  222. std::cout << "task2.1" << std::endl;
  223. throw 21;
  224. });
  225. });
  226. });
  227. int i = 0, j = 10, k = 20;
  228. trh.run([=, &task3_flag]()
  229. {
  230. task3_flag = true;
  231. std::cout << "task3: " << i << " " << j << " " << k << std::endl;
  232. });
  233. std::cout << "parent" << std::endl;
  234. });
  235. }
  236. catch (exception_list const& el)
  237. {
  238. BOOST_TEST(el.size() == 1u);
  239. for (boost::exception_ptr const& e: el)
  240. {
  241. try {
  242. boost::rethrow_exception(e);
  243. }
  244. catch (int i) // this doesn't works yet
  245. {
  246. BOOST_TEST_EQ(i, 21);
  247. }
  248. catch (boost::exception&)
  249. {
  250. BOOST_TEST(true);
  251. }
  252. catch (...)
  253. {
  254. BOOST_TEST(false);
  255. }
  256. }
  257. }
  258. catch (...)
  259. {
  260. BOOST_TEST(false);
  261. }
  262. BOOST_TEST(parent_flag);
  263. BOOST_TEST(task1_flag);
  264. BOOST_TEST(task2_flag);
  265. BOOST_TEST(task21_flag);
  266. }
  267. int main()
  268. {
  269. run_no_exception();
  270. run_no_exception_wait();
  271. run_exception();
  272. run_exception_1();
  273. run_nested_exception();
  274. return boost::report_errors();
  275. }
  276. #else
  277. int main()
  278. {
  279. return boost::report_errors();
  280. }
  281. #endif