test_12293.cpp 1.8 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. // BoostFutureTest.cpp : Defines the entry point for the console application.
  7. //
  8. #include <iostream>
  9. // boost version 1.60.0
  10. // has the following set.
  11. #define BOOST_THREAD_VERSION 4
  12. // #define BOOST_THREAD_PROVIDES_EXECUTORS
  13. #include <boost/thread/future.hpp>
  14. int main()
  15. {
  16. #if __cplusplus >= 201103L
  17. int value = 0;
  18. int tmpValue = 0;
  19. boost::promise<void> promise1;
  20. boost::promise<void> promise2;
  21. auto future1 = promise1.get_future();
  22. auto waitFuture = future1.then([&tmpValue, &promise2](boost::future<void> future){
  23. assert(future.is_ready()); // this works correctly and is ready.
  24. auto fut = boost::async(boost::launch::async, [&promise2, &tmpValue](){
  25. boost::this_thread::sleep_for(boost::chrono::seconds(1));
  26. tmpValue = 1;
  27. promise2.set_value();
  28. std::cout << "Step 2 "<< std::endl; // should print 1 but prints 0
  29. });
  30. std::cout << "Step 1 "<< std::endl; // should print 1 but prints 0
  31. return promise2.get_future();
  32. //return ;
  33. }).then([&value, &tmpValue](boost::future<boost::future<void>> future){
  34. //}).then([&value, &tmpValue](boost::future<void> future){
  35. // error: no matching function for call to ‘boost::future<boost::future<void> >::then(main()::<lambda(boost::future<void>)>)’
  36. // as expected
  37. assert(future.is_ready()); // this doesn't work correctly and is not ready.
  38. value = tmpValue;
  39. });
  40. promise1.set_value();
  41. waitFuture.wait();
  42. std::cout << "value = " << value << std::endl; // should print 1 but prints 0
  43. #endif
  44. return 0;
  45. }