1 //  Copyright (c) 2013 Hartmut Kaiser
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 // This example demonstrates the use of the utility function
7 // make_ready_future_after to orchestrate timed operations with 'normal'
8 // asynchronous work.
9 
10 #include <hpx/hpx_init.hpp>
11 #include <hpx/include/iostreams.hpp>
12 #include <hpx/util/high_resolution_timer.hpp>
13 
14 #include <chrono>
15 
16 ///////////////////////////////////////////////////////////////////////////////
wake_up_after_2_seconds()17 void wake_up_after_2_seconds()
18 {
19     hpx::cout << "waiting for 2 seconds\n";
20 
21     hpx::util::high_resolution_timer t;
22 
23     // Schedule a wakeup after 2 seconds.
24     using std::chrono::seconds;
25     hpx::future<void> f = hpx::make_ready_future_after(seconds(2));
26 
27     // ... do other things while waiting for the future to get ready
28 
29     // wait until the new future gets ready
30     f.wait();
31 
32     hpx::cout << "woke up after " << t.elapsed()
33               << " seconds\n" << hpx::flush;
34 }
35 
return_int_at_time()36 int return_int_at_time()
37 {
38     hpx::cout << "generating an 'int' value 2 seconds from now\n";
39 
40     hpx::util::high_resolution_timer t;
41 
42     // Schedule a wakeup 2 seconds from now.
43     using namespace std::chrono;
44     hpx::future<int> f = hpx::make_ready_future_at(
45         steady_clock::now() + seconds(2), 42);
46 
47     // ... do other things while waiting for the future to get ready
48 
49     // wait until the new future gets ready (should return 42)
50     int retval = f.get();
51 
52     hpx::cout << "woke up after " << t.elapsed()
53               << " seconds, returned: " << retval << "\n"
54               << hpx::flush;
55 
56     return retval;
57 }
58 
59 ///////////////////////////////////////////////////////////////////////////////
hpx_main()60 int hpx_main()
61 {
62     wake_up_after_2_seconds();
63     return_int_at_time();
64     return hpx::finalize();
65 }
66 
67 ///////////////////////////////////////////////////////////////////////////////
main(int argc,char * argv[])68 int main(int argc, char* argv[])
69 {
70     // Initialize and run HPX.
71     return hpx::init(argc, argv);
72 }
73 
74