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