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