Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of Boost. Click here to view this page for the latest version.
PrevUpHomeNext

basic_deadline_timer

Provides waitable timer functionality.

template<
    typename Time,
    typename TimeTraits = boost::asio::time_traits<Time>,
    typename TimerService = deadline_timer_service<Time, TimeTraits>>
class basic_deadline_timer :
  public basic_io_object< TimerService >
Types

Name

Description

duration_type

The duration type.

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_type

The time type.

traits_type

The time traits type.

Member Functions

Name

Description

async_wait

Start an asynchronous wait on the timer.

basic_deadline_timer

Constructor.

cancel

Cancel any asynchronous operations that are waiting on the timer.

expires_at

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

expires_from_now

Get the timer's expiry time relative to now.

get_io_service

Get the io_service associated with the object.

io_service

(Deprecated: use get_io_service().) Get the io_service associated with the object.

wait

Perform a blocking wait on the timer.

Protected Data Members

Name

Description

implementation

The underlying implementation of the I/O object.

service

The service associated with the I/O object.

The basic_deadline_timer class template provides the ability to perform a blocking or asynchronous wait for a timer to expire.

Most applications will use the boost::asio::deadline_timer typedef.

Thread Safety

Distinct objects: Safe.

Shared objects: Unsafe.

Examples

Performing a blocking wait:

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

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

...

// Construct a timer with an absolute expiry time.
boost::asio::deadline_timer timer(io_service,
    boost::posix_time::time_from_string("2005-12-07 23:59:59.000"));

// Start an asynchronous wait.
timer.async_wait(handler);
Changing an active deadline_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_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 boost::system::error_code& e)
{
  if (e != boost::asio::error::operation_aborted)
  {
    // Timer was not cancelled, take necessary action.
  }
}

PrevUpHomeNext