timers.qbk 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. [/
  2. / Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  3. /
  4. / Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. /]
  7. [section:timers Timers]
  8. Long running I/O operations will often have a deadline by which they must have
  9. completed. These deadlines may be expressed as absolute times, but are often
  10. calculated relative to the current time.
  11. As a simple example, to perform a synchronous wait operation on a timer using a
  12. relative time one may write:
  13. io_context i;
  14. ...
  15. deadline_timer t(i);
  16. t.expires_from_now(boost::posix_time::seconds(5));
  17. t.wait();
  18. More commonly, a program will perform an asynchronous wait operation on a
  19. timer:
  20. void handler(boost::system::error_code ec) { ... }
  21. ...
  22. io_context i;
  23. ...
  24. deadline_timer t(i);
  25. t.expires_from_now(boost::posix_time::milliseconds(400));
  26. t.async_wait(handler);
  27. ...
  28. i.run();
  29. The deadline associated with a timer may also be obtained as a relative time:
  30. boost::posix_time::time_duration time_until_expiry
  31. = t.expires_from_now();
  32. or as an absolute time to allow composition of timers:
  33. deadline_timer t2(i);
  34. t2.expires_at(t.expires_at() + boost::posix_time::seconds(30));
  35. [heading See Also]
  36. [link boost_asio.reference.basic_deadline_timer basic_deadline_timer],
  37. [link boost_asio.reference.deadline_timer deadline_timer],
  38. [link boost_asio.tutorial.tuttimer1 timer tutorials].
  39. [endsect]