lambda_future.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright (C) 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_CONTINUATION \
  18. && ! defined BOOST_NO_CXX11_LAMBDAS && ! (defined BOOST_MSVC && _MSC_VER < 1800) // works since msvc-12.0
  19. #ifdef BOOST_MSVC
  20. #pragma warning(disable: 4127) // conditional expression is constant
  21. #endif
  22. int main()
  23. {
  24. const int number_of_tests = 100;
  25. BOOST_THREAD_LOG << "<MAIN" << BOOST_THREAD_END_LOG;
  26. for (int i=0; i< number_of_tests; i++)
  27. try
  28. {
  29. {
  30. boost::future<int> f1 = boost::async(boost::launch::async, []() {return 123;});
  31. int result = f1.get();
  32. BOOST_THREAD_LOG << "f1 " << result << BOOST_THREAD_END_LOG;
  33. }
  34. {
  35. boost::future<int> f1 = boost::async(boost::launch::async, []() {return 123;});
  36. boost::future<int> f2 = f1.then([](boost::future<int> f) {return 2*f.get(); });
  37. int result = f2.get();
  38. BOOST_THREAD_LOG << "f2 " << result << BOOST_THREAD_END_LOG;
  39. }
  40. #if ! defined BOOST_NO_CXX14_GENERIC_LAMBDAS
  41. {
  42. boost::future<int> f1 = boost::async(boost::launch::async, []() {return 123;});
  43. boost::future<int> f2 = f1.then([](auto f) {return 2*f.get(); });
  44. int result = f2.get();
  45. BOOST_THREAD_LOG << "f2 " << result << BOOST_THREAD_END_LOG;
  46. }
  47. #endif
  48. }
  49. catch (std::exception& ex)
  50. {
  51. std::cout << "ERRORRRRR "<<ex.what() << "" << std::endl;
  52. BOOST_THREAD_LOG << "ERRORRRRR "<<ex.what() << "" << BOOST_THREAD_END_LOG;
  53. return 1;
  54. }
  55. catch (...)
  56. {
  57. std::cout << " ERRORRRRR exception thrown" << std::endl;
  58. BOOST_THREAD_LOG << " ERRORRRRR exception thrown" << BOOST_THREAD_END_LOG;
  59. return 2;
  60. }
  61. BOOST_THREAD_LOG << "MAIN>" << BOOST_THREAD_END_LOG;
  62. return 0;
  63. }
  64. #else
  65. //#warning "This test is not supported in this configuration, either because Bosst.Thread has been configured to don't support continuations, the compiler doesn't provides lambdas or because they are buggy as for MSV versions < msvc-12.0"
  66. int main()
  67. {
  68. return 0;
  69. }
  70. #endif