asio C++ library

PrevUpHomeNext

basic_waitable_timer

Provides waitable timer functionality.

template<
    typename Clock,
    typename WaitTraits = asio::wait_traits<Clock>,
    typename WaitableTimerService = waitable_timer_service<Clock, WaitTraits>>
class basic_waitable_timer :
  public basic_io_object< WaitableTimerService >
Types

Name

Description

clock_type

The clock type.

duration

The duration type of the clock.

executor_type

The type of the executor associated with the object.

implementation_type

The underlying implementation type of I/O object.

service_type

The type of the service that will be used to provide I/O operations.

time_point

The time point type of the clock.

traits_type

The wait traits type.

Member Functions

Name

Description

async_wait

Start an asynchronous wait on the timer.

basic_waitable_timer

Constructor.

Constructor to set a particular expiry time as an absolute time.

Constructor to set a particular expiry time relative to now.

Move-construct a basic_waitable_timer from another.

cancel

Cancel any asynchronous operations that are waiting on the timer.

cancel_one

Cancels one asynchronous operation that is waiting on the timer.

expires_after

Set the timer's expiry time relative to now.

expires_at

(Deprecated: Use expiry().) Get the timer's expiry time as an absolute time.

Set the timer's expiry time as an absolute time.

expires_from_now

(Deprecated: Use expiry().) Get the timer's expiry time relative to now.

(Deprecated: Use expires_after().) Set the timer's expiry time relative to now.

expiry

Get the timer's expiry time as an absolute time.

get_executor

Get the executor associated with the object.

get_io_service

(Deprecated: Use get_executor().) Get the io_service associated with the object.

operator=

Move-assign a basic_waitable_timer from another.

wait

Perform a blocking wait on the timer.

Protected Member Functions

Name

Description

get_implementation

Get the underlying implementation of the I/O object.

get_service

Get the service associated with the I/O object.

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.

Remarks

This waitable timer functionality is for use with the C++11 standard library's <chrono> facility, or with the Boost.Chrono library.

Thread Safety

Distinct objects: Safe.

Shared objects: Unsafe.

Examples

Performing a blocking wait (C++11):

// Construct a timer without setting an expiry time.
asio::steady_timer timer(io_service);

// 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 asio::error_code& error)
{
  if (!error)
  {
    // Timer expired.
  }
}

...

// Construct a timer with an absolute expiry time.
asio::steady_timer timer(io_service,
    std::chrono::steady_clock::now() + std::chrono::seconds(60));

// Start an asynchronous wait.
timer.async_wait(handler);
Changing an active waitable timer's expiry time

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 asio::error_code& e)
{
  if (e != asio::error::operation_aborted)
  {
    // Timer was not cancelled, take necessary action.
  }
}
Requirements

Header: asio/basic_waitable_timer.hpp

Convenience header: asio.hpp


PrevUpHomeNext