test_11818.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright (C) 2014 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. #define BOOST_THREAD_VERSION 4
  6. #include <boost/config.hpp>
  7. #if ! defined BOOST_NO_CXX11_DECLTYPE
  8. #define BOOST_RESULT_OF_USE_DECLTYPE
  9. #endif
  10. #include <boost/thread/future.hpp>
  11. #include <boost/thread/thread.hpp>
  12. #include <thread>
  13. int main()
  14. {
  15. {
  16. boost::promise<int> promise;
  17. boost::future<int> future = promise.get_future();
  18. boost::future<int> result =
  19. future.then
  20. (
  21. boost::launch::deferred,
  22. [](boost::future<int> && f)
  23. {
  24. std::cout << std::this_thread::get_id() << ": callback" << std::endl;
  25. std::cout << "The value is: " << f.get() << std::endl;
  26. return f.get();
  27. }
  28. );
  29. // We could not reach here.
  30. std::cout << std::this_thread::get_id() << ": function" << std::endl;
  31. promise.set_value(0);
  32. }
  33. {
  34. boost::promise<int> promise;
  35. boost::shared_future<int> future = promise.get_future().share();
  36. boost::future<int> result =
  37. future.then
  38. (
  39. boost::launch::deferred,
  40. [](boost::shared_future<int> && f)
  41. {
  42. std::cout << std::this_thread::get_id() << ": callback" << std::endl;
  43. std::cout << "The value is: " << f.get() << std::endl;
  44. return f.get();
  45. }
  46. );
  47. // We could not reach here.
  48. std::cout << std::this_thread::get_id() << ": function" << std::endl;
  49. promise.set_value(0);
  50. }
  51. return 0;
  52. }