]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // time_t_timer.cpp | |
3 | // ~~~~~~~~~~~~~~~~ | |
4 | // | |
92f5a8d4 | 5 | // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
7c673cae FG |
6 | // |
7 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
8 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
9 | // | |
10 | ||
11 | #include <boost/asio.hpp> | |
12 | #include <ctime> | |
13 | #include <iostream> | |
14 | ||
11fdf7f2 TL |
15 | // A custom implementation of the Clock concept from the standard C++ library. |
16 | struct time_t_clock | |
7c673cae | 17 | { |
7c673cae | 18 | // The duration type. |
11fdf7f2 | 19 | typedef boost::asio::chrono::steady_clock::duration duration; |
7c673cae | 20 | |
11fdf7f2 TL |
21 | // The duration's underlying arithmetic representation. |
22 | typedef duration::rep rep; | |
7c673cae | 23 | |
11fdf7f2 TL |
24 | // The ratio representing the duration's tick period. |
25 | typedef duration::period period; | |
26 | ||
27 | // An absolute time point represented using the clock. | |
28 | typedef boost::asio::chrono::time_point<time_t_clock> time_point; | |
29 | ||
30 | // The clock is not monotonically increasing. | |
31 | static const bool is_steady = false; | |
7c673cae | 32 | |
11fdf7f2 TL |
33 | // Get the current time. |
34 | static time_point now() | |
7c673cae | 35 | { |
11fdf7f2 | 36 | return time_point() + boost::asio::chrono::seconds(std::time(0)); |
7c673cae | 37 | } |
11fdf7f2 | 38 | }; |
7c673cae | 39 | |
11fdf7f2 TL |
40 | // The boost::asio::basic_waitable_timer template accepts an optional WaitTraits |
41 | // template parameter. The underlying time_t clock has one-second granularity, | |
42 | // so these traits may be customised to reduce the latency between the clock | |
43 | // ticking over and a wait operation's completion. When the timeout is near | |
44 | // (less than one second away) we poll the clock more frequently to detect the | |
45 | // time change closer to when it occurs. The user can select the appropriate | |
46 | // trade off between accuracy and the increased CPU cost of polling. In extreme | |
47 | // cases, a zero duration may be returned to make the timers as accurate as | |
48 | // possible, albeit with 100% CPU usage. | |
49 | struct time_t_wait_traits | |
50 | { | |
51 | // Determine how long until the clock should be next polled to determine | |
52 | // whether the duration has elapsed. | |
53 | static time_t_clock::duration to_wait_duration( | |
54 | const time_t_clock::duration& d) | |
7c673cae | 55 | { |
11fdf7f2 TL |
56 | if (d > boost::asio::chrono::seconds(1)) |
57 | return d - boost::asio::chrono::seconds(1); | |
58 | else if (d > boost::asio::chrono::seconds(0)) | |
59 | return boost::asio::chrono::milliseconds(10); | |
60 | else | |
61 | return boost::asio::chrono::seconds(0); | |
7c673cae FG |
62 | } |
63 | ||
11fdf7f2 TL |
64 | // Determine how long until the clock should be next polled to determine |
65 | // whether the absoluate time has been reached. | |
66 | static time_t_clock::duration to_wait_duration( | |
67 | const time_t_clock::time_point& t) | |
7c673cae | 68 | { |
11fdf7f2 | 69 | return to_wait_duration(t - time_t_clock::now()); |
7c673cae FG |
70 | } |
71 | }; | |
72 | ||
11fdf7f2 TL |
73 | typedef boost::asio::basic_waitable_timer< |
74 | time_t_clock, time_t_wait_traits> time_t_timer; | |
7c673cae FG |
75 | |
76 | void handle_timeout(const boost::system::error_code&) | |
77 | { | |
78 | std::cout << "handle_timeout\n"; | |
79 | } | |
80 | ||
81 | int main() | |
82 | { | |
83 | try | |
84 | { | |
b32b8144 | 85 | boost::asio::io_context io_context; |
7c673cae | 86 | |
b32b8144 | 87 | time_t_timer timer(io_context); |
7c673cae | 88 | |
11fdf7f2 | 89 | timer.expires_after(boost::asio::chrono::seconds(5)); |
7c673cae FG |
90 | std::cout << "Starting synchronous wait\n"; |
91 | timer.wait(); | |
92 | std::cout << "Finished synchronous wait\n"; | |
93 | ||
11fdf7f2 | 94 | timer.expires_after(boost::asio::chrono::seconds(5)); |
7c673cae FG |
95 | std::cout << "Starting asynchronous wait\n"; |
96 | timer.async_wait(&handle_timeout); | |
b32b8144 | 97 | io_context.run(); |
7c673cae FG |
98 | std::cout << "Finished asynchronous wait\n"; |
99 | } | |
100 | catch (std::exception& e) | |
101 | { | |
102 | std::cout << "Exception: " << e.what() << "\n"; | |
103 | } | |
104 | ||
105 | return 0; | |
106 | } |