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_main.hpp>
11 #include <hpx/include/threads.hpp>
12 #include <hpx/include/util.hpp>
13 #include <hpx/include/iostreams.hpp>
14 
15 #include <chrono>
16 
17 ///////////////////////////////////////////////////////////////////////////////
call_every_500_millisecs()18 bool call_every_500_millisecs()
19 {
20     static int counter = 0;
21 
22     hpx::cout << "Callback " << ++counter << std::endl;
23     return counter != 10;     // stop timer after 10 invocations
24 }
25 
main()26 int main()
27 {
28     {
29         // initialize timer to invoke given function every 500 milliseconds
30         hpx::util::interval_timer timer(
31             &call_every_500_millisecs, std::chrono::milliseconds(500)
32         );
33 
34         timer.start();
35 
36         // wait for timer to have invoked the function 10 times
37         while (!timer.is_terminated())
38             hpx::this_thread::yield();
39     }
40 
41     return hpx::finalize();
42 }
43 
44