async_executor_pass.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // Copyright (C) 2011 Vicente J. Botet Escriba
  10. //
  11. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  12. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  13. // <boost/thread/future.hpp>
  14. // template <class Executor, class F, class... Args>
  15. // future<typename result_of<F(Args...)>::type>
  16. // async(Executor& ex, F&& f, Args&&... args);
  17. #define BOOST_THREAD_VERSION 5
  18. #include <boost/config.hpp>
  19. #if ! defined BOOST_NO_CXX11_DECLTYPE
  20. #define BOOST_RESULT_OF_USE_DECLTYPE
  21. #endif
  22. #include <iostream>
  23. #include <boost/thread/future.hpp>
  24. #include <boost/thread/thread.hpp>
  25. #include <boost/thread/detail/memory.hpp>
  26. #include <boost/thread/csbl/memory/unique_ptr.hpp>
  27. #include <memory>
  28. #include <boost/detail/lightweight_test.hpp>
  29. #include <boost/thread/executors/basic_thread_pool.hpp>
  30. #include <boost/thread/executor.hpp>
  31. typedef boost::chrono::high_resolution_clock Clock;
  32. typedef boost::chrono::milliseconds ms;
  33. class A
  34. {
  35. long data_;
  36. public:
  37. typedef long result_type;
  38. explicit A(long i) :
  39. data_(i)
  40. {
  41. }
  42. long doit() const
  43. {
  44. boost::this_thread::sleep_for(ms(200));
  45. return data_;
  46. }
  47. long operator()() const
  48. {
  49. boost::this_thread::sleep_for(ms(200));
  50. return data_;
  51. }
  52. };
  53. class MoveOnly
  54. {
  55. public:
  56. typedef int result_type;
  57. int value;
  58. BOOST_THREAD_MOVABLE_ONLY(MoveOnly)
  59. MoveOnly()
  60. {
  61. value = 0;
  62. }
  63. MoveOnly( BOOST_THREAD_RV_REF(MoveOnly))
  64. {
  65. value = 1;
  66. }
  67. MoveOnly& operator=(BOOST_THREAD_RV_REF(MoveOnly))
  68. {
  69. value = 2;
  70. return *this;
  71. }
  72. int operator()()
  73. {
  74. boost::this_thread::sleep_for(ms(200));
  75. return 3;
  76. }
  77. template <typename OS>
  78. friend OS& operator<<(OS& os, MoveOnly const& v)
  79. {
  80. os << v.value;
  81. return os;
  82. }
  83. };
  84. namespace boost
  85. {
  86. BOOST_THREAD_DCL_MOVABLE (MoveOnly)
  87. }
  88. int f0()
  89. {
  90. boost::this_thread::sleep_for(ms(200));
  91. return 3;
  92. }
  93. int i = 0;
  94. int& f1()
  95. {
  96. boost::this_thread::sleep_for(ms(200));
  97. return i;
  98. }
  99. void f2()
  100. {
  101. boost::this_thread::sleep_for(ms(200));
  102. }
  103. boost::csbl::unique_ptr<int> f3_0()
  104. {
  105. boost::this_thread::sleep_for(ms(200));
  106. boost::csbl::unique_ptr<int> r( (new int(3)));
  107. return boost::move(r);
  108. }
  109. MoveOnly f3_1()
  110. {
  111. boost::this_thread::sleep_for(ms(200));
  112. MoveOnly r;
  113. return boost::move(r);
  114. }
  115. boost::csbl::unique_ptr<int> f3(int i)
  116. {
  117. boost::this_thread::sleep_for(ms(200));
  118. return boost::csbl::unique_ptr<int>(new int(i));
  119. }
  120. boost::csbl::unique_ptr<int> f4(
  121. BOOST_THREAD_RV_REF_BEG boost::csbl::unique_ptr<int> BOOST_THREAD_RV_REF_END p
  122. )
  123. {
  124. boost::this_thread::sleep_for(ms(200));
  125. return boost::move(p);
  126. }
  127. struct check_timer {
  128. boost::chrono::nanoseconds delay;
  129. Clock::time_point start;
  130. check_timer(boost::chrono::nanoseconds delay)
  131. : delay(delay)
  132. , start(Clock::now())
  133. {
  134. }
  135. ~check_timer() {
  136. Clock::time_point now = Clock::now();
  137. BOOST_TEST(now - start < delay);
  138. std::cout << __FILE__ << "[" << __LINE__ << "] " << (now - start).count() << std::endl;
  139. }
  140. };
  141. int main()
  142. {
  143. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  144. #if defined BOOST_THREAD_PROVIDES_EXECUTORS
  145. {
  146. try
  147. {
  148. boost::executor_adaptor<boost::basic_thread_pool> ex(1);
  149. boost::future<int> f = boost::async(ex, &f0);
  150. boost::this_thread::sleep_for(ms(300));
  151. int res;
  152. {
  153. check_timer timer(ms(500));
  154. res = f.get();
  155. }
  156. BOOST_TEST(res == 3);
  157. }
  158. catch (std::exception& ex)
  159. {
  160. std::cout << __FILE__ << "[" << __LINE__ << "]" << ex.what() << std::endl;
  161. BOOST_TEST(false && "exception thrown");
  162. }
  163. catch (...)
  164. {
  165. BOOST_TEST(false && "exception thrown");
  166. }
  167. }
  168. #endif
  169. #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) && defined BOOST_THREAD_PROVIDES_EXECUTORS
  170. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  171. {
  172. try
  173. {
  174. boost::executor_adaptor<boost::basic_thread_pool> ex(1);
  175. boost::future<long> f = boost::async(ex, A(3));
  176. boost::this_thread::sleep_for(ms(300));
  177. int res;
  178. {
  179. check_timer timer(ms(500));
  180. res = f.get();
  181. }
  182. BOOST_TEST(res == 3);
  183. }
  184. catch (std::exception& ex)
  185. {
  186. std::cout << __FILE__ << "[" << __LINE__ << "]" << ex.what() << std::endl;
  187. BOOST_TEST(false && "exception thrown");
  188. }
  189. catch (...)
  190. {
  191. BOOST_TEST(false && "exception thrown");
  192. }
  193. }
  194. #endif
  195. #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) && defined BOOST_THREAD_PROVIDES_EXECUTORS
  196. std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
  197. {
  198. try
  199. {
  200. boost::executor_adaptor<boost::basic_thread_pool> ex(1);
  201. MoveOnly mo;
  202. boost::future<int> f = boost::async(ex, boost::move(mo));
  203. //boost::future<int> f = boost::async(ex, MoveOnly());
  204. boost::this_thread::sleep_for(ms(300));
  205. int res;
  206. {
  207. check_timer timer(ms(500));
  208. res = f.get();
  209. }
  210. BOOST_TEST(res == 3);
  211. }
  212. catch (std::exception& ex)
  213. {
  214. std::cout << __FILE__ << "[" << __LINE__ << "]" << ex.what() << std::endl;
  215. BOOST_TEST(false && "exception thrown");
  216. }
  217. catch (...)
  218. {
  219. BOOST_TEST(false && "exception thrown");
  220. }
  221. }
  222. #endif
  223. return boost::report_errors();
  224. }