src/​examples/​cpp03/​timers/​time_t_timer.​cppsrc/​examples/​cpp11/​timers/​time_t_timer.​cpp
1 /​/​1 /​/​
2 /​/​·​time_t_timer.​cpp2 /​/​·​time_t_timer.​cpp
3 /​/​·​~~~~~~~~~~~~~~~~3 /​/​·​~~~~~~~~~~~~~~~~
4 /​/​4 /​/​
5 /​/​·​Copyright·​(c)​·​2003-​2022·​Christopher·​M.​·​Kohlhoff·​(chris·​at·​kohlhoff·​dot·​com)​5 /​/​·​Copyright·​(c)​·​2003-​2022·​Christopher·​M.​·​Kohlhoff·​(chris·​at·​kohlhoff·​dot·​com)​
6 /​/​6 /​/​
7 /​/​·​Distributed·​under·​the·​Boost·​Software·​License,​·​Version·​1.​0.​·​(See·​accompanying7 /​/​·​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)​8 /​/​·​file·​LICENSE_1_0.​txt·​or·​copy·​at·​http:​/​/​www.​boost.​org/​LICENSE_1_0.​txt)​
9 /​/​9 /​/​
10 10
11 #include·​<asio.​hpp>11 #include·​<asio.​hpp>
12 #include·​<ctime>12 #include·​<ctime>
13 #include·​<chrono>
13 #include·​<iostream>14 #include·​<iostream>
14 15
15 /​/​·​A·​custom·​implementation·​of·​the·​Clock·​concept·​from·​the·​standard·​C++·​library.​16 /​/​·​A·​custom·​implementation·​of·​the·​Clock·​concept·​from·​the·​standard·​C++·​library.​
16 struct·​time_t_clock17 struct·​time_t_clock
17 {18 {
18 ··​/​/​·​The·​duration·​type.​19 ··​/​/​·​The·​duration·​type.​
19 ··​typedef·asio:​:​chrono:​:​steady_clock:​:​duration·​duration;​20 ··​typedef·​std:​:​chrono:​:​steady_clock:​:​duration·​duration;​
20 21
21 ··​/​/​·​The·​duration's·​underlying·​arithmetic·​representation.​22 ··​/​/​·​The·​duration's·​underlying·​arithmetic·​representation.​
22 ··​typedef·​duration:​:​rep·​rep;​23 ··​typedef·​duration:​:​rep·​rep;​
23 24
24 ··​/​/​·​The·​ratio·​representing·​the·​duration's·​tick·​period.​25 ··​/​/​·​The·​ratio·​representing·​the·​duration's·​tick·​period.​
25 ··​typedef·​duration:​:​period·​period;​26 ··​typedef·​duration:​:​period·​period;​
26 27
27 ··​/​/​·​An·​absolute·​time·​point·​represented·​using·​the·​clock.​28 ··​/​/​·​An·​absolute·​time·​point·​represented·​using·​the·​clock.​
28 ··​typedef·asio:​:​chrono:​:​time_point<time_t_clo​ck>·​time_point;​29 ··​typedef·​std:​:​chrono:​:​time_point<time_t_clo​ck>·​time_point;​
29 30
30 ··​/​/​·​The·​clock·​is·​not·​monotonically·​increasing.​31 ··​/​/​·​The·​clock·​is·​not·​monotonically·​increasing.​
31 ··​static·​const·​bool·​is_steady·​=·​false;​32 ··​static·​constexpr·​bool·​is_steady·​=·​false;​
32 33
33 ··​/​/​·​Get·​the·​current·​time.​34 ··​/​/​·​Get·​the·​current·​time.​
34 ··​static·​time_point·​now()​35 ··​static·​time_point·​now()​·noexcept
35 ··​{36 ··​{
36 ····​return·​time_point()​·​+·asio:​:​chrono:​:​seconds(std:​:​time(0)​)​;​37 ····​return·​time_point()​·​+·​std:​:​chrono:​:​seconds(std:​:​time(0)​)​;​
37 ··​}38 ··​}
38 };​39 };​
39 40
40 /​/​·​The·​asio:​:​basic_waitable_timer·​template·​accepts·​an·​optional·​WaitTraits41 /​/​·​The·​asio:​:​basic_waitable_timer·​template·​accepts·​an·​optional·​WaitTraits
41 /​/​·​template·​parameter.​·​The·​underlying·​time_t·​clock·​has·​one-​second·​granularity,​42 /​/​·​template·​parameter.​·​The·​underlying·​time_t·​clock·​has·​one-​second·​granularity,​
42 /​/​·​so·​these·​traits·​may·​be·​customised·​to·​reduce·​the·​latency·​between·​the·​clock43 /​/​·​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·​near44 /​/​·​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·​the45 /​/​·​(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·​appropriate46 /​/​·​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·​extreme47 /​/​·​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·​as48 /​/​·​cases,​·​a·​zero·​duration·​may·​be·​returned·​to·​make·​the·​timers·​as·​accurate·​as
48 /​/​·​possible,​·​albeit·​with·​100%·​CPU·​usage.​49 /​/​·​possible,​·​albeit·​with·​100%·​CPU·​usage.​
49 struct·​time_t_wait_traits50 struct·​time_t_wait_traits
50 {51 {
51 ··​/​/​·​Determine·​how·​long·​until·​the·​clock·​should·​be·​next·​polled·​to·​determine52 ··​/​/​·​Determine·​how·​long·​until·​the·​clock·​should·​be·​next·​polled·​to·​determine
52 ··​/​/​·​whether·​the·​duration·​has·​elapsed.​53 ··​/​/​·​whether·​the·​duration·​has·​elapsed.​
53 ··​static·​time_t_clock:​:​duration·​to_wait_duration(54 ··​static·​time_t_clock:​:​duration·​to_wait_duration(
54 ······​const·​time_t_clock:​:​duration&·​d)​55 ······​const·​time_t_clock:​:​duration&·​d)​
55 ··​{56 ··​{
56 ····​if·​(d·​>·asio:​:​chrono:​:​seconds(1)​)​57 ····​if·​(d·​>·​std:​:​chrono:​:​seconds(1)​)​
57 ······​return·​d·​-​·asio:​:​chrono:​:​seconds(1)​;​58 ······​return·​d·​-​·​std:​:​chrono:​:​seconds(1)​;​
58 ····​else·​if·​(d·​>·asio:​:​chrono:​:​seconds(0)​)​59 ····​else·​if·​(d·​>·​std:​:​chrono:​:​seconds(0)​)​
59 ······​return·asio:​:​chrono:​:​milliseconds(10)​;​60 ······​return·​std:​:​chrono:​:​milliseconds(10)​;​
60 ····​else61 ····​else
61 ······​return·asio:​:​chrono:​:​seconds(0)​;​62 ······​return·​std:​:​chrono:​:​seconds(0)​;​
62 ··​}63 ··​}
63 64
64 ··​/​/​·​Determine·​how·​long·​until·​the·​clock·​should·​be·​next·​polled·​to·​determine65 ··​/​/​·​Determine·​how·​long·​until·​the·​clock·​should·​be·​next·​polled·​to·​determine
65 ··​/​/​·​whether·​the·​absoluate·​time·​has·​been·​reached.​66 ··​/​/​·​whether·​the·​absoluate·​time·​has·​been·​reached.​
66 ··​static·​time_t_clock:​:​duration·​to_wait_duration(67 ··​static·​time_t_clock:​:​duration·​to_wait_duration(
67 ······​const·​time_t_clock:​:​time_point&·​t)​68 ······​const·​time_t_clock:​:​time_point&·​t)​
68 ··​{69 ··​{
69 ····​return·​to_wait_duration(t·​-​·​time_t_clock:​:​now()​)​;​70 ····​return·​to_wait_duration(t·​-​·​time_t_clock:​:​now()​)​;​
70 ··​}71 ··​}
71 };​72 };​
72 73
73 typedef·​asio:​:​basic_waitable_timer<​74 typedef·​asio:​:​basic_waitable_timer<​
74 ··​time_t_clock,​·​time_t_wait_traits>·​time_t_timer;​75 ··​time_t_clock,​·​time_t_wait_traits>·​time_t_timer;​
75 76
76 void·​handle_timeout(const·​asio:​:​error_code&)​
77 {
78 ··​std:​:​cout·​<<·​"handle_timeout\n";​
79 }
80
81 int·​main()​77 int·​main()​
82 {78 {
83 ··​try79 ··​try
84 ··​{80 ··​{
85 ····​asio:​:​io_context·​io_context;​81 ····​asio:​:​io_context·​io_context;​
86 82
87 ····​time_t_timer·​timer(io_context)​;​83 ····​time_t_timer·​timer(io_context)​;​
88 84
89 ····​timer.​expires_after(asio:​:​chrono:​:​seconds(5)​)​;​85 ····​timer.​expires_after(std:​:​chrono:​:​seconds(5)​)​;​
90 ····​std:​:​cout·​<<·​"Starting·​synchronous·​wait\n";​86 ····​std:​:​cout·​<<·​"Starting·​synchronous·​wait\n";​
91 ····​timer.​wait()​;​87 ····​timer.​wait()​;​
92 ····​std:​:​cout·​<<·​"Finished·​synchronous·​wait\n";​88 ····​std:​:​cout·​<<·​"Finished·​synchronous·​wait\n";​
93 89
94 ····​timer.​expires_after(asio:​:​chrono:​:​seconds(5)​)​;​90 ····​timer.​expires_after(std:​:​chrono:​:​seconds(5)​)​;​
95 ····​std:​:​cout·​<<·​"Starting·​asynchronous·​wait\n";​91 ····​std:​:​cout·​<<·​"Starting·​asynchronous·​wait\n";​
96 ····​timer.​async_wait(&handle_ti​meout)​;​92 ····​timer.​async_wait(
93 ········​[](const·​std:​:​error_code&·​/​*error*/​)​
94 ········​{
95 ··········​std:​:​cout·​<<·​"timeout\n";​
96 ········​})​;​
97 ····​io_context.​run()​;​97 ····​io_context.​run()​;​
98 ····​std:​:​cout·​<<·​"Finished·​asynchronous·​wait\n";​98 ····​std:​:​cout·​<<·​"Finished·​asynchronous·​wait\n";​
99 ··​}99 ··​}
100 ··​catch·​(std:​:​exception&·​e)​100 ··​catch·​(std:​:​exception&·​e)​
101 ··​{101 ··​{
102 ····​std:​:​cout·​<<·​"Exception:​·​"·​<<·​e.​what()​·​<<·​"\n";​102 ····​std:​:​cout·​<<·​"Exception:​·​"·​<<·​e.​what()​·​<<·​"\n";​
103 ··​}103 ··​}
104 104
105 ··​return·​0;​105 ··​return·​0;​
106 }106 }