![]() |
Home | Libraries | People | FAQ | More |
Typedef for a timer based on the high resolution clock.
typedef basic_waitable_timer< chrono::high_resolution_clock > high_resolution_timer;
Name |
Description |
---|---|
Rebinds the timer type to another executor. |
|
The clock type. |
|
The duration type of the clock. |
|
The type of the executor associated with the object. |
|
The time point type of the clock. |
|
The wait 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.
|
|
Set the timer's expiry time relative to now. |
|
(Deprecated: Use expiry().) Get the timer's expiry time as an absolute
time. |
|
(Deprecated: Use expiry().) Get the timer's expiry time relative
to now. |
|
Get the timer's expiry time as an absolute time. |
|
Get the executor associated with the object. |
|
Move-assign a basic_waitable_timer from another. |
|
Perform a blocking wait on the timer. |
|
Destroys the timer. |
The basic_waitable_timer
class template provides the ability to perform a blocking or asynchronous
wait for a timer to expire.
A waitable 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 one of the steady_timer
,
system_timer
or high_resolution_timer
typedefs.
This waitable timer functionality is for use with the C++11 standard library's
<chrono>
facility, or with the Boost.Chrono library.
Distinct objects: Safe.
Shared objects: Unsafe.
Performing a blocking wait (C++11):
// Construct a timer without setting an expiry time. boost::asio::steady_timer timer(my_context); // Set an expiry time relative to now. timer.expires_after(std::chrono::seconds(5)); // Wait for the timer to expire. timer.wait();
Performing an asynchronous wait (C++11):
void handler(const boost::system::error_code& error) { if (!error) { // Timer expired. } } ... // Construct a timer with an absolute expiry time. boost::asio::steady_timer timer(my_context, std::chrono::steady_clock::now() + std::chrono::seconds(60)); // 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_after(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 boost::system::error_code& e) { if (e != boost::asio::error::operation_aborted) { // Timer was not cancelled, take necessary action. } }
boost::asio::basic_waitable_timer::expires_after()
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.
boost::asio::error::operation_aborted
.
This typedef uses the C++11 <chrono>
standard library
facility, if available. Otherwise, it may use the Boost.Chrono library. To
explicitly utilise Boost.Chrono, use the basic_waitable_timer
template directly:
typedef basic_waitable_timer<boost::chrono::high_resolution_clock> timer;
Header: boost/asio/high_resolution_timer.hpp
Convenience header: boost/asio.hpp