make_future.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright (C) 2012 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. #if ! defined BOOST_NO_CXX11_DECLTYPE
  7. #define BOOST_RESULT_OF_USE_DECLTYPE
  8. #endif
  9. #define BOOST_THREAD_VERSION 4
  10. #include <boost/thread/future.hpp>
  11. #include <iostream>
  12. namespace boost
  13. {
  14. template <typename T>
  15. exception_ptr make_exception_ptr(T v)
  16. {
  17. return copy_exception(v);
  18. }
  19. }
  20. int p1() { return 5; }
  21. int& p1r() { static int i=0; return i; }
  22. void p() { }
  23. #if defined BOOST_THREAD_USES_MOVE
  24. boost::future<void> void_compute()
  25. {
  26. return BOOST_THREAD_MAKE_RV_REF(boost::make_ready_future());
  27. }
  28. #endif
  29. boost::future<int> compute(int x)
  30. {
  31. if (x == 0) return boost::make_ready_future(0);
  32. #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
  33. if (x < 0) return boost::make_exceptional_future<int>(std::logic_error("Error"));
  34. #else
  35. if (x < 0) return boost::make_exceptional(std::logic_error("Error"));
  36. #endif
  37. //boost::future<int> f1 = boost::async([]() { return x+1; });
  38. boost::future<int> f1 = boost::async(p1);
  39. return boost::move(f1);
  40. }
  41. boost::future<int&> compute_ref(int x)
  42. {
  43. static int i = 0;
  44. //if (x == 0) return boost::make_ready_future<int&>(i); //This must not compile as the type is deduced as boost::future<int>
  45. if (x == 0) return boost::make_ready_no_decay_future<int&>(i);
  46. #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
  47. if (x < 0) return boost::make_exceptional_future<int&>(std::logic_error("Error"));
  48. #else
  49. if (x < 0) return boost::make_exceptional(std::logic_error("Error"));
  50. #endif
  51. boost::future<int&> f1 = boost::async(p1r);
  52. return boost::move(f1);
  53. }
  54. boost::shared_future<int> shared_compute(int x)
  55. {
  56. if (x == 0) return boost::make_ready_future(0).share();
  57. #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
  58. if (x < 0) return boost::make_exceptional_future<int>(std::logic_error("Error")).share();
  59. #else
  60. if (x < 0) return boost::make_exceptional(std::logic_error("Error"));
  61. #endif
  62. //boost::future<int> f1 = boost::async([]() { return x+1; });
  63. boost::shared_future<int> f1 = boost::async(&p1).share();
  64. return f1;
  65. }
  66. int main()
  67. {
  68. const int number_of_tests = 100;
  69. for (int i=0; i< number_of_tests; i++)
  70. try
  71. {
  72. // {
  73. // std::cout << __FILE__ << " "<<__LINE__ << std::endl;
  74. // boost::future<int> f = boost::async(boost::launch::async, p1);
  75. // std::cout << i << " "<<f.get() << std::endl;
  76. // }
  77. #if defined BOOST_THREAD_USES_MOVE
  78. {
  79. std::cout << __FILE__ << " "<< __LINE__ << std::endl;
  80. boost::future<void> f = void_compute();
  81. f.get();
  82. }
  83. #endif
  84. {
  85. std::cout << __FILE__ << " "<< __LINE__ << std::endl;
  86. boost::future<int> f = compute(-1);
  87. f.wait();
  88. }
  89. {
  90. std::cout << __FILE__ << " "<< __LINE__ << std::endl;
  91. boost::future<int> f = compute(0);
  92. std::cout << f.get() << std::endl;
  93. }
  94. {
  95. std::cout << __FILE__ << " "<< __LINE__ << std::endl;
  96. boost::future<int&> f = compute_ref(0);
  97. std::cout << f.get() << std::endl;
  98. }
  99. #if __cplusplus > 201103L
  100. {
  101. std::cout << __FILE__ << " "<< __LINE__ << std::endl;
  102. int i = 0;
  103. boost::future<int&> f = boost::make_ready_future(std::ref(i));
  104. std::cout << f.get() << std::endl;
  105. }
  106. #endif
  107. {
  108. std::cout << __FILE__ << " "<< __LINE__ << std::endl;
  109. int i = 0;
  110. boost::future<int&> f = boost::make_ready_future(boost::ref(i));
  111. std::cout << f.get() << std::endl;
  112. }
  113. {
  114. std::cout << __FILE__ << " "<< __LINE__ << std::endl;
  115. const int i = 0;
  116. boost::future<int const&> f = boost::make_ready_future(boost::cref(i));
  117. std::cout << f.get() << std::endl;
  118. }
  119. {
  120. std::cout << __FILE__ << " "<< __LINE__ << std::endl;
  121. boost::future<int> f = compute(2);
  122. std::cout << f.get() << std::endl;
  123. }
  124. {
  125. std::cout << __FILE__ << " "<< __LINE__ << std::endl;
  126. boost::shared_future<int> f = shared_compute(0);
  127. std::cout << f.get() << std::endl;
  128. }
  129. }
  130. catch (std::exception& ex)
  131. {
  132. std::cout << "ERRORRRRR "<<ex.what() << "" << std::endl;
  133. return 1;
  134. }
  135. catch (...)
  136. {
  137. std::cout << "ERRORRRRR "<<"ERRORRRRR exception thrown" << std::endl;
  138. return 2;
  139. }
  140. return 0;
  141. }