time_t_timer.cpp 3.2 KB

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