]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/asio/example/cpp03/timers/time_t_timer.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / asio / example / cpp03 / timers / time_t_timer.cpp
1 //
2 // time_t_timer.cpp
3 // ~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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
15 struct time_t_traits
16 {
17 // The time type.
18 typedef std::time_t time_type;
19
20 // The duration type.
21 struct duration_type
22 {
23 duration_type() : value(0) {}
24 duration_type(std::time_t v) : value(v) {}
25 std::time_t value;
26 };
27
28 // Get the current time.
29 static time_type now()
30 {
31 return std::time(0);
32 }
33
34 // Add a duration to a time.
35 static time_type add(const time_type& t, const duration_type& d)
36 {
37 return t + d.value;
38 }
39
40 // Subtract one time from another.
41 static duration_type subtract(const time_type& t1, const time_type& t2)
42 {
43 return duration_type(t1 - t2);
44 }
45
46 // Test whether one time is less than another.
47 static bool less_than(const time_type& t1, const time_type& t2)
48 {
49 return t1 < t2;
50 }
51
52 // Convert to POSIX duration type.
53 static boost::posix_time::time_duration to_posix_duration(
54 const duration_type& d)
55 {
56 return boost::posix_time::seconds(d.value);
57 }
58 };
59
60 typedef boost::asio::basic_deadline_timer<
61 std::time_t, time_t_traits> time_t_timer;
62
63 void handle_timeout(const boost::system::error_code&)
64 {
65 std::cout << "handle_timeout\n";
66 }
67
68 int main()
69 {
70 try
71 {
72 boost::asio::io_context io_context;
73
74 time_t_timer timer(io_context);
75
76 timer.expires_from_now(5);
77 std::cout << "Starting synchronous wait\n";
78 timer.wait();
79 std::cout << "Finished synchronous wait\n";
80
81 timer.expires_from_now(5);
82 std::cout << "Starting asynchronous wait\n";
83 timer.async_wait(&handle_timeout);
84 io_context.run();
85 std::cout << "Finished asynchronous wait\n";
86 }
87 catch (std::exception& e)
88 {
89 std::cout << "Exception: " << e.what() << "\n";
90 }
91
92 return 0;
93 }