future_unwrap.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright (C) 2012-2013 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. //#define BOOST_THREAD_USES_LOG
  11. #define BOOST_THREAD_USES_LOG_THREAD_ID
  12. #include <boost/thread/detail/log.hpp>
  13. #include <boost/thread/future.hpp>
  14. #include <boost/assert.hpp>
  15. #include <string>
  16. #include <iostream>
  17. #if defined BOOST_THREAD_PROVIDES_FUTURE_UNWRAP
  18. #ifdef BOOST_MSVC
  19. #pragma warning(disable: 4127) // conditional expression is constant
  20. #endif
  21. int p1()
  22. {
  23. BOOST_THREAD_LOG << "P1" << BOOST_THREAD_END_LOG;
  24. return 123;
  25. }
  26. boost::future<int> p2()
  27. {
  28. BOOST_THREAD_LOG << "<P2" << BOOST_THREAD_END_LOG;
  29. boost::future<int> f1 = boost::async(boost::launch::async, &p1);
  30. BOOST_THREAD_LOG << "P2>" << BOOST_THREAD_END_LOG;
  31. return boost::move(f1);
  32. }
  33. int main()
  34. {
  35. const int number_of_tests = 100;
  36. BOOST_THREAD_LOG << "<MAIN" << BOOST_THREAD_END_LOG;
  37. for (int i=0; i< number_of_tests; i++)
  38. try
  39. {
  40. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  41. {
  42. boost::future<int> inner_future = boost::async(boost::launch::async, &p2).unwrap();
  43. inner_future.wait();
  44. int ii = inner_future.get();
  45. BOOST_THREAD_LOG << "ii= "<< ii << "" << BOOST_THREAD_END_LOG;
  46. }
  47. #endif
  48. {
  49. boost::future<boost::future<int> > outer_future = boost::async(boost::launch::async, &p2);
  50. boost::future<int> inner_future = outer_future.unwrap();
  51. inner_future.wait();
  52. int ii = inner_future.get();
  53. BOOST_THREAD_LOG << "ii= "<< ii << "" << BOOST_THREAD_END_LOG;
  54. }
  55. {
  56. boost::future<boost::future<int> > outer_future = boost::async(boost::launch::async, &p2);
  57. boost::future<int> inner_future = outer_future.unwrap();
  58. int ii = inner_future.get();
  59. BOOST_THREAD_LOG << "ii= "<< ii << "" << BOOST_THREAD_END_LOG;
  60. }
  61. }
  62. catch (std::exception& ex)
  63. {
  64. std::cout << "ERRORRRRR "<<ex.what() << "" << std::endl;
  65. BOOST_THREAD_LOG << "ERRORRRRR "<<ex.what() << "" << BOOST_THREAD_END_LOG;
  66. return 1;
  67. }
  68. catch (...)
  69. {
  70. std::cout << " ERRORRRRR exception thrown" << std::endl;
  71. BOOST_THREAD_LOG << " ERRORRRRR exception thrown" << BOOST_THREAD_END_LOG;
  72. return 2;
  73. }
  74. BOOST_THREAD_LOG << "MAIN>" << BOOST_THREAD_END_LOG;
  75. return 0;
  76. }
  77. #else
  78. int main()
  79. {
  80. return 0;
  81. }
  82. #endif