test_ml.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Copyright (C) 2010 Vicente Botet
  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. #include <boost/config.hpp>
  6. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  7. #include <boost/detail/lightweight_test.hpp>
  8. #include <boost/thread/future.hpp>
  9. #include <boost/utility/result_of.hpp>
  10. #include <functional>
  11. struct async_func {
  12. virtual ~async_func() { }
  13. virtual void run() =0;
  14. };
  15. template <typename Ret>
  16. class async_func_pt : public async_func {
  17. boost::packaged_task<Ret> f;
  18. public:
  19. void run() {
  20. std::cout << __FILE__ << ":" << __LINE__ << std::endl;
  21. f(); }
  22. async_func_pt (boost::packaged_task<Ret>&& f) : f(boost::move(f)) {}
  23. ~async_func_pt() { }
  24. boost::unique_future<Ret> get_future() { return f.get_future(); }
  25. };
  26. void async_core (async_func* p) {
  27. std::cout << __FILE__ << ":" << __LINE__ << std::endl;
  28. p->run();
  29. }
  30. template <typename F>
  31. boost::unique_future<typename boost::result_of< F() >::type>
  32. async (F&& f)
  33. {
  34. std::cout << __FILE__ << ":" << __LINE__ << std::endl;
  35. typedef typename boost::result_of< F() >::type RetType;
  36. std::cout << __FILE__ << ":" << __LINE__ << std::endl;
  37. async_func_pt<RetType>* p= new async_func_pt<RetType> (boost::packaged_task<RetType>(boost::forward<F>(f)));
  38. std::cout << __FILE__ << ":" << __LINE__ << std::endl;
  39. boost::unique_future<RetType> future_result= p->get_future();
  40. std::cout << __FILE__ << ":" << __LINE__ << std::endl;
  41. async_core (p);
  42. std::cout << __FILE__ << ":" << __LINE__ << std::endl;
  43. return boost::move(future_result);
  44. }
  45. template <typename F, typename A1>
  46. boost::unique_future<typename boost::result_of< F(A1) >::type>
  47. async (F&& f, A1&& a1)
  48. {
  49. std::cout << __FILE__ << ":" << __LINE__ << std::endl;
  50. // This should be all it needs. But get a funny error deep inside Boost.
  51. // problem overloading with && ?
  52. return async (boost::bind(f,a1));
  53. }
  54. int calculate_the_answer_to_life_the_universe_and_everything()
  55. {
  56. return 42;
  57. }
  58. size_t foo (const std::string& s)
  59. {
  60. return s.size();
  61. }
  62. void test1()
  63. {
  64. // this one works
  65. // most fundimental form:
  66. std::cout << __FILE__ << ":" << __LINE__ << std::endl;
  67. boost::unique_future<int> fi= async (&calculate_the_answer_to_life_the_universe_and_everything);
  68. int i= fi.get();
  69. BOOST_TEST (i== 42);
  70. // This one chokes at compile time
  71. std::cout << __FILE__ << ":" << __LINE__ << std::endl;
  72. boost::unique_future<size_t> fut_1= async (&foo, "Life");
  73. BOOST_TEST (fut_1.get()== 4);
  74. }
  75. int main()
  76. {
  77. test1();
  78. return boost::report_errors();
  79. }
  80. #else
  81. int main()
  82. {
  83. return 0;
  84. }
  85. #endif
  86. /*
  87. *
  88. "/Users/viboes/clang/llvmCore-3.0-rc1.install/bin/clang++"
  89. -o "../../../bin.v2/libs/thread/test/test_ml.test/clang-darwin-3.0x/debug/threading-multi/test_ml"
  90. "../../../bin.v2/libs/thread/test/test_ml.test/clang-darwin-3.0x/debug/threading-multi/test_ml.o"
  91. "../../../bin.v2/libs/test/build/clang-darwin-3.0x/debug/link-static/threading-multi/libboost_unit_test_framework.a"
  92. "../../../bin.v2/libs/thread/build/clang-darwin-3.0x/debug/threading-multi/libboost_thread.dylib"
  93. "../../../bin.v2/libs/chrono/build/clang-darwin-3.0x/debug/threading-multi/libboost_chrono.dylib"
  94. "../../../bin.v2/libs/system/build/clang-darwin-3.0x/debug/threading-multi/libboost_system.dylib" -g
  95. */
  96. /*
  97. *
  98. * #include <boost/test/unit_test.hpp>
  99. #include <boost/thread/future.hpp>
  100. #include <boost/utility/result_of.hpp>
  101. #include <functional>
  102. struct async_func {
  103. virtual ~async_func() { }
  104. virtual void run() =0;
  105. };
  106. template <typename Ret>
  107. class async_func_pt : public async_func {
  108. boost::packaged_task<Ret> f;
  109. public:
  110. void run() override { f(); }
  111. async_func_pt (boost::packaged_task<Ret>&& f) : f(std::move(f)) {}
  112. ~async_func_pt() { }
  113. boost::unique_future<Ret> get_future() { return f.get_future(); }
  114. };
  115. void async_core (async_func* p);
  116. template <typename F>
  117. boost::unique_future<typename boost::result_of< F() >::type>
  118. async (F&& f)
  119. {
  120. typedef typename boost::result_of< F() >::type RetType;
  121. async_func_pt<RetType>* p= new async_func_pt<RetType> (boost::packaged_task<RetType>(f));
  122. boost::unique_future<RetType> future_result= p->get_future();
  123. async_core (p);
  124. return std::move(future_result);
  125. }
  126. template <typename F, typename A1>
  127. boost::unique_future<typename boost::result_of< F(A1) >::type>
  128. async (F&& f, A1&& a1)
  129. {
  130. // This should be all it needs. But get a funny error deep inside Boost.
  131. // problem overloading with && ?
  132. return async (std::tr1::bind(f,a1));
  133. }
  134. BOOST_AUTO_TEST_SUITE(thread_pool_tests)
  135. int calculate_the_answer_to_life_the_universe_and_everything()
  136. {
  137. return 42;
  138. }
  139. size_t foo (const std::string& s)
  140. {
  141. return s.size();
  142. }
  143. BOOST_AUTO_TEST_CASE( async_test )
  144. {
  145. // this one works
  146. // most fundimental form:
  147. boost::unique_future<int> fi= async (&calculate_the_answer_to_life_the_universe_and_everything);
  148. int i= fi.get();
  149. BOOST_CHECK_EQUAL (i, 42);
  150. // This one chokes at compile time
  151. boost::unique_future<size_t> fut_1= async (&foo, "Life");
  152. BOOST_CHECK_EQUAL (fut_1.get(), 4);
  153. }
  154. BOOST_AUTO_TEST_SUITE_END()
  155. */