Typedef for the typical usage of timer. Uses a UTC clock.
typedef basic_deadline_timer< boost::posix_time::ptime > deadline_timer;
Name |
Description |
---|---|
The duration type. |
|
The type of the executor associated with the object. |
|
The time type. |
|
The time traits type. |
Name |
Description |
---|---|
Start an asynchronous wait on the timer. |
|
Constructor. |
|
Cancel any asynchronous operations that are waiting on the timer. |
|
Cancels one asynchronous operation that is waiting on the timer. |
|
Get the timer's expiry time as an absolute time. |
|
Get the timer's expiry time relative to now. |
|
Get the executor associated with the object. |
|
(Deprecated: Use get_executor().) Get the io_context associated with the object. |
|
(Deprecated: Use get_executor().) Get the io_context associated with the object. |
|
Move-assign a basic_deadline_timer from another. |
|
Perform a blocking wait on the timer. |
|
Destroys the timer. |
The basic_deadline_timer
class template provides the ability to perform a blocking or asynchronous
wait for a timer to expire.
A deadline timer is always in one of two states: "expired" or "not
expired". If the wait()
or async_wait()
function
is called on an expired timer, the wait operation will complete immediately.
Most applications will use the deadline_timer
typedef.
Distinct objects: Safe.
Shared objects: Unsafe.
Performing a blocking wait:
// Construct a timer without setting an expiry time. asio::deadline_timer timer(io_context); // Set an expiry time relative to now. timer.expires_from_now(boost::posix_time::seconds(5)); // Wait for the timer to expire. timer.wait();
Performing an asynchronous wait:
void handler(const asio::error_code& error) { if (!error) { // Timer expired. } } ... // Construct a timer with an absolute expiry time. asio::deadline_timer timer(io_context, boost::posix_time::time_from_string("2005-12-07 23:59:59.000")); // Start an asynchronous wait. timer.async_wait(handler);
Changing the expiry time of a timer while there are pending asynchronous waits causes those wait operations to be cancelled. To ensure that the action associated with the timer is performed only once, use something like this: used:
void on_some_event() { if (my_timer.expires_from_now(seconds(5)) > 0) { // We managed to cancel the timer. Start new asynchronous wait. my_timer.async_wait(on_timeout); } else { // Too late, timer has already expired! } } void on_timeout(const asio::error_code& e) { if (e != asio::error::operation_aborted) { // Timer was not cancelled, take necessary action. } }
asio::basic_deadline_timer::expires_from_now()
function
cancels any pending asynchronous waits, and returns the number of asynchronous
waits that were cancelled. If it returns 0 then you were too late and
the wait handler has already been executed, or will soon be executed.
If it returns 1 then the wait handler was successfully cancelled.
error_code
passed to it contains the value asio::error::operation_aborted
.
Header: asio/deadline_timer.hpp
Convenience header: asio.hpp