Clementine
Classes | Public Types | Public Member Functions | List of all members
asio::basic_waitable_timer< Clock, WaitTraits, Executor > Class Template Reference

Provides waitable timer functionality. More...

#include <basic_waitable_timer.hpp>

Classes

struct  rebind_executor
 Rebinds the timer type to another executor. More...
 

Public Types

typedef Executor executor_type
 The type of the executor associated with the object.
 
typedef Clock clock_type
 The clock type.
 
typedef clock_type::duration duration
 The duration type of the clock.
 
typedef clock_type::time_point time_point
 The time point type of the clock.
 
typedef WaitTraits traits_type
 The wait traits type.
 

Public Member Functions

 basic_waitable_timer (const executor_type &ex)
 Constructor. More...
 
template<typename ExecutionContext >
 basic_waitable_timer (ExecutionContext &context, typename enable_if< is_convertible< ExecutionContext &, execution_context &>::value >::type *=0)
 Constructor. More...
 
 basic_waitable_timer (const executor_type &ex, const time_point &expiry_time)
 Constructor to set a particular expiry time as an absolute time. More...
 
template<typename ExecutionContext >
 basic_waitable_timer (ExecutionContext &context, const time_point &expiry_time, typename enable_if< is_convertible< ExecutionContext &, execution_context &>::value >::type *=0)
 Constructor to set a particular expiry time as an absolute time. More...
 
 basic_waitable_timer (const executor_type &ex, const duration &expiry_time)
 Constructor to set a particular expiry time relative to now. More...
 
template<typename ExecutionContext >
 basic_waitable_timer (ExecutionContext &context, const duration &expiry_time, typename enable_if< is_convertible< ExecutionContext &, execution_context &>::value >::type *=0)
 Constructor to set a particular expiry time relative to now. More...
 
 ~basic_waitable_timer ()
 Destroys the timer. More...
 
executor_type get_executor () ASIO_NOEXCEPT
 Get the executor associated with the object.
 
std::size_t cancel ()
 Cancel any asynchronous operations that are waiting on the timer. More...
 
std::size_t cancel (asio::error_code &ec)
 (Deprecated: Use non-error_code overload.) Cancel any asynchronous operations that are waiting on the timer. More...
 
std::size_t cancel_one ()
 Cancels one asynchronous operation that is waiting on the timer. More...
 
std::size_t cancel_one (asio::error_code &ec)
 (Deprecated: Use non-error_code overload.) Cancels one asynchronous operation that is waiting on the timer. More...
 
time_point expires_at () const
 (Deprecated: Use expiry().) Get the timer's expiry time as an absolute time. More...
 
time_point expiry () const
 Get the timer's expiry time as an absolute time. More...
 
std::size_t expires_at (const time_point &expiry_time)
 Set the timer's expiry time as an absolute time. More...
 
std::size_t expires_at (const time_point &expiry_time, asio::error_code &ec)
 (Deprecated: Use non-error_code overload.) Set the timer's expiry time as an absolute time. More...
 
std::size_t expires_after (const duration &expiry_time)
 Set the timer's expiry time relative to now. More...
 
duration expires_from_now () const
 (Deprecated: Use expiry().) Get the timer's expiry time relative to now. More...
 
std::size_t expires_from_now (const duration &expiry_time)
 (Deprecated: Use expires_after().) Set the timer's expiry time relative to now. More...
 
std::size_t expires_from_now (const duration &expiry_time, asio::error_code &ec)
 (Deprecated: Use expires_after().) Set the timer's expiry time relative to now. More...
 
void wait ()
 Perform a blocking wait on the timer. More...
 
void wait (asio::error_code &ec)
 Perform a blocking wait on the timer. More...
 
template<WaitHandler ASIO_DEFAULT_COMPLETION_TOKEN_TYPE>
 ASIO_INITFN_AUTO_RESULT_TYPE (WaitHandler, void(asio::error_code)) async_wait(ASIO_MOVE_ARG(WaitHandler) handler ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
 Start an asynchronous wait on the timer. More...
 

Detailed Description

template<typename Clock, typename WaitTraits, typename Executor>
class asio::basic_waitable_timer< Clock, WaitTraits, Executor >

Provides waitable timer functionality.

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 asio::steady_timer, asio::system_timer or asio::high_resolution_timer typedefs.

Note
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(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 asio::error_code& error)
{
if (!error)
{
// Timer expired.
}
}
...
// Construct a timer with an absolute expiry time.
asio::steady_timer timer(my_context,
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.
}
}

Constructor & Destructor Documentation

◆ basic_waitable_timer() [1/6]

template<typename Clock , typename WaitTraits , typename Executor >
asio::basic_waitable_timer< Clock, WaitTraits, Executor >::basic_waitable_timer ( const executor_type ex)
inlineexplicit

Constructor.

This constructor creates a timer without setting an expiry time. The expires_at() or expires_after() functions must be called to set an expiry time before the timer can be waited on.

Parameters
exThe I/O executor that the timer will use, by default, to dispatch handlers for any asynchronous operations performed on the timer.

◆ basic_waitable_timer() [2/6]

template<typename Clock , typename WaitTraits , typename Executor >
template<typename ExecutionContext >
asio::basic_waitable_timer< Clock, WaitTraits, Executor >::basic_waitable_timer ( ExecutionContext &  context,
typename enable_if< is_convertible< ExecutionContext &, execution_context &>::value >::type *  = 0 
)
inlineexplicit

Constructor.

This constructor creates a timer without setting an expiry time. The expires_at() or expires_after() functions must be called to set an expiry time before the timer can be waited on.

Parameters
contextAn execution context which provides the I/O executor that the timer will use, by default, to dispatch handlers for any asynchronous operations performed on the timer.

◆ basic_waitable_timer() [3/6]

template<typename Clock , typename WaitTraits , typename Executor >
asio::basic_waitable_timer< Clock, WaitTraits, Executor >::basic_waitable_timer ( const executor_type ex,
const time_point expiry_time 
)
inline

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

This constructor creates a timer and sets the expiry time.

Parameters
exThe I/O executor object that the timer will use, by default, to dispatch handlers for any asynchronous operations performed on the timer.
expiry_timeThe expiry time to be used for the timer, expressed as an absolute time.

◆ basic_waitable_timer() [4/6]

template<typename Clock , typename WaitTraits , typename Executor >
template<typename ExecutionContext >
asio::basic_waitable_timer< Clock, WaitTraits, Executor >::basic_waitable_timer ( ExecutionContext &  context,
const time_point expiry_time,
typename enable_if< is_convertible< ExecutionContext &, execution_context &>::value >::type *  = 0 
)
inlineexplicit

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

This constructor creates a timer and sets the expiry time.

Parameters
contextAn execution context which provides the I/O executor that the timer will use, by default, to dispatch handlers for any asynchronous operations performed on the timer.
expiry_timeThe expiry time to be used for the timer, expressed as an absolute time.

◆ basic_waitable_timer() [5/6]

template<typename Clock , typename WaitTraits , typename Executor >
asio::basic_waitable_timer< Clock, WaitTraits, Executor >::basic_waitable_timer ( const executor_type ex,
const duration expiry_time 
)
inline

Constructor to set a particular expiry time relative to now.

This constructor creates a timer and sets the expiry time.

Parameters
exThe I/O executor that the timer will use, by default, to dispatch handlers for any asynchronous operations performed on the timer.
expiry_timeThe expiry time to be used for the timer, relative to now.

◆ basic_waitable_timer() [6/6]

template<typename Clock , typename WaitTraits , typename Executor >
template<typename ExecutionContext >
asio::basic_waitable_timer< Clock, WaitTraits, Executor >::basic_waitable_timer ( ExecutionContext &  context,
const duration expiry_time,
typename enable_if< is_convertible< ExecutionContext &, execution_context &>::value >::type *  = 0 
)
inlineexplicit

Constructor to set a particular expiry time relative to now.

This constructor creates a timer and sets the expiry time.

Parameters
contextAn execution context which provides the I/O executor that the timer will use, by default, to dispatch handlers for any asynchronous operations performed on the timer.
expiry_timeThe expiry time to be used for the timer, relative to now.

◆ ~basic_waitable_timer()

template<typename Clock , typename WaitTraits , typename Executor >
asio::basic_waitable_timer< Clock, WaitTraits, Executor >::~basic_waitable_timer ( )
inline

Destroys the timer.

This function destroys the timer, cancelling any outstanding asynchronous wait operations associated with the timer as if by calling cancel.

Member Function Documentation

◆ ASIO_INITFN_AUTO_RESULT_TYPE()

template<typename Clock , typename WaitTraits , typename Executor >
template<WaitHandler ASIO_DEFAULT_COMPLETION_TOKEN_TYPE>
asio::basic_waitable_timer< Clock, WaitTraits, Executor >::ASIO_INITFN_AUTO_RESULT_TYPE ( WaitHandler  ,
void(asio::error_code  
)
inline

Start an asynchronous wait on the timer.

This function may be used to initiate an asynchronous wait against the timer. It always returns immediately.

For each call to async_wait(), the supplied handler will be called exactly once. The handler will be called when:

  • The timer has expired.
  • The timer was cancelled, in which case the handler is passed the error code asio::error::operation_aborted.
Parameters
handlerThe handler to be called when the timer expires. Copies will be made of the handler as required. The function signature of the handler must be:
void handler(
const asio::error_code& error // Result of operation.
);
Regardless of whether the asynchronous operation completes immediately or not, the handler will not be invoked from within this function. On immediate completion, invocation of the handler will be performed in a manner equivalent to using asio::post().

◆ cancel() [1/2]

template<typename Clock , typename WaitTraits , typename Executor >
std::size_t asio::basic_waitable_timer< Clock, WaitTraits, Executor >::cancel ( )
inline

Cancel any asynchronous operations that are waiting on the timer.

This function forces the completion of any pending asynchronous wait operations against the timer. The handler for each cancelled operation will be invoked with the asio::error::operation_aborted error code.

Cancelling the timer does not change the expiry time.

Returns
The number of asynchronous operations that were cancelled.
Exceptions
asio::system_errorThrown on failure.
Note
If the timer has already expired when cancel() is called, then the handlers for asynchronous wait operations will:
  • have already been invoked; or
  • have been queued for invocation in the near future.

These handlers can no longer be cancelled, and therefore are passed an error code that indicates the successful completion of the wait operation.

◆ cancel() [2/2]

template<typename Clock , typename WaitTraits , typename Executor >
std::size_t asio::basic_waitable_timer< Clock, WaitTraits, Executor >::cancel ( asio::error_code ec)
inline

(Deprecated: Use non-error_code overload.) Cancel any asynchronous operations that are waiting on the timer.

This function forces the completion of any pending asynchronous wait operations against the timer. The handler for each cancelled operation will be invoked with the asio::error::operation_aborted error code.

Cancelling the timer does not change the expiry time.

Parameters
ecSet to indicate what error occurred, if any.
Returns
The number of asynchronous operations that were cancelled.
Note
If the timer has already expired when cancel() is called, then the handlers for asynchronous wait operations will:
  • have already been invoked; or
  • have been queued for invocation in the near future.

These handlers can no longer be cancelled, and therefore are passed an error code that indicates the successful completion of the wait operation.

◆ cancel_one() [1/2]

template<typename Clock , typename WaitTraits , typename Executor >
std::size_t asio::basic_waitable_timer< Clock, WaitTraits, Executor >::cancel_one ( )
inline

Cancels one asynchronous operation that is waiting on the timer.

This function forces the completion of one pending asynchronous wait operation against the timer. Handlers are cancelled in FIFO order. The handler for the cancelled operation will be invoked with the asio::error::operation_aborted error code.

Cancelling the timer does not change the expiry time.

Returns
The number of asynchronous operations that were cancelled. That is, either 0 or 1.
Exceptions
asio::system_errorThrown on failure.
Note
If the timer has already expired when cancel_one() is called, then the handlers for asynchronous wait operations will:
  • have already been invoked; or
  • have been queued for invocation in the near future.

These handlers can no longer be cancelled, and therefore are passed an error code that indicates the successful completion of the wait operation.

◆ cancel_one() [2/2]

template<typename Clock , typename WaitTraits , typename Executor >
std::size_t asio::basic_waitable_timer< Clock, WaitTraits, Executor >::cancel_one ( asio::error_code ec)
inline

(Deprecated: Use non-error_code overload.) Cancels one asynchronous operation that is waiting on the timer.

This function forces the completion of one pending asynchronous wait operation against the timer. Handlers are cancelled in FIFO order. The handler for the cancelled operation will be invoked with the asio::error::operation_aborted error code.

Cancelling the timer does not change the expiry time.

Parameters
ecSet to indicate what error occurred, if any.
Returns
The number of asynchronous operations that were cancelled. That is, either 0 or 1.
Note
If the timer has already expired when cancel_one() is called, then the handlers for asynchronous wait operations will:
  • have already been invoked; or
  • have been queued for invocation in the near future.

These handlers can no longer be cancelled, and therefore are passed an error code that indicates the successful completion of the wait operation.

◆ expires_after()

template<typename Clock , typename WaitTraits , typename Executor >
std::size_t asio::basic_waitable_timer< Clock, WaitTraits, Executor >::expires_after ( const duration expiry_time)
inline

Set the timer's expiry time relative to now.

This function sets the expiry time. Any pending asynchronous wait operations will be cancelled. The handler for each cancelled operation will be invoked with the asio::error::operation_aborted error code.

Parameters
expiry_timeThe expiry time to be used for the timer.
Returns
The number of asynchronous operations that were cancelled.
Exceptions
asio::system_errorThrown on failure.
Note
If the timer has already expired when expires_after() is called, then the handlers for asynchronous wait operations will:
  • have already been invoked; or
  • have been queued for invocation in the near future.

These handlers can no longer be cancelled, and therefore are passed an error code that indicates the successful completion of the wait operation.

◆ expires_at() [1/3]

template<typename Clock , typename WaitTraits , typename Executor >
time_point asio::basic_waitable_timer< Clock, WaitTraits, Executor >::expires_at ( ) const
inline

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

This function may be used to obtain the timer's current expiry time. Whether the timer has expired or not does not affect this value.

◆ expires_at() [2/3]

template<typename Clock , typename WaitTraits , typename Executor >
std::size_t asio::basic_waitable_timer< Clock, WaitTraits, Executor >::expires_at ( const time_point expiry_time)
inline

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

This function sets the expiry time. Any pending asynchronous wait operations will be cancelled. The handler for each cancelled operation will be invoked with the asio::error::operation_aborted error code.

Parameters
expiry_timeThe expiry time to be used for the timer.
Returns
The number of asynchronous operations that were cancelled.
Exceptions
asio::system_errorThrown on failure.
Note
If the timer has already expired when expires_at() is called, then the handlers for asynchronous wait operations will:
  • have already been invoked; or
  • have been queued for invocation in the near future.

These handlers can no longer be cancelled, and therefore are passed an error code that indicates the successful completion of the wait operation.

◆ expires_at() [3/3]

template<typename Clock , typename WaitTraits , typename Executor >
std::size_t asio::basic_waitable_timer< Clock, WaitTraits, Executor >::expires_at ( const time_point expiry_time,
asio::error_code ec 
)
inline

(Deprecated: Use non-error_code overload.) Set the timer's expiry time as an absolute time.

This function sets the expiry time. Any pending asynchronous wait operations will be cancelled. The handler for each cancelled operation will be invoked with the asio::error::operation_aborted error code.

Parameters
expiry_timeThe expiry time to be used for the timer.
ecSet to indicate what error occurred, if any.
Returns
The number of asynchronous operations that were cancelled.
Note
If the timer has already expired when expires_at() is called, then the handlers for asynchronous wait operations will:
  • have already been invoked; or
  • have been queued for invocation in the near future.

These handlers can no longer be cancelled, and therefore are passed an error code that indicates the successful completion of the wait operation.

◆ expires_from_now() [1/3]

template<typename Clock , typename WaitTraits , typename Executor >
duration asio::basic_waitable_timer< Clock, WaitTraits, Executor >::expires_from_now ( ) const
inline

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

This function may be used to obtain the timer's current expiry time. Whether the timer has expired or not does not affect this value.

◆ expires_from_now() [2/3]

template<typename Clock , typename WaitTraits , typename Executor >
std::size_t asio::basic_waitable_timer< Clock, WaitTraits, Executor >::expires_from_now ( const duration expiry_time)
inline

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

This function sets the expiry time. Any pending asynchronous wait operations will be cancelled. The handler for each cancelled operation will be invoked with the asio::error::operation_aborted error code.

Parameters
expiry_timeThe expiry time to be used for the timer.
Returns
The number of asynchronous operations that were cancelled.
Exceptions
asio::system_errorThrown on failure.
Note
If the timer has already expired when expires_from_now() is called, then the handlers for asynchronous wait operations will:
  • have already been invoked; or
  • have been queued for invocation in the near future.

These handlers can no longer be cancelled, and therefore are passed an error code that indicates the successful completion of the wait operation.

◆ expires_from_now() [3/3]

template<typename Clock , typename WaitTraits , typename Executor >
std::size_t asio::basic_waitable_timer< Clock, WaitTraits, Executor >::expires_from_now ( const duration expiry_time,
asio::error_code ec 
)
inline

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

This function sets the expiry time. Any pending asynchronous wait operations will be cancelled. The handler for each cancelled operation will be invoked with the asio::error::operation_aborted error code.

Parameters
expiry_timeThe expiry time to be used for the timer.
ecSet to indicate what error occurred, if any.
Returns
The number of asynchronous operations that were cancelled.
Note
If the timer has already expired when expires_from_now() is called, then the handlers for asynchronous wait operations will:
  • have already been invoked; or
  • have been queued for invocation in the near future.

These handlers can no longer be cancelled, and therefore are passed an error code that indicates the successful completion of the wait operation.

◆ expiry()

template<typename Clock , typename WaitTraits , typename Executor >
time_point asio::basic_waitable_timer< Clock, WaitTraits, Executor >::expiry ( ) const
inline

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

This function may be used to obtain the timer's current expiry time. Whether the timer has expired or not does not affect this value.

◆ wait() [1/2]

template<typename Clock , typename WaitTraits , typename Executor >
void asio::basic_waitable_timer< Clock, WaitTraits, Executor >::wait ( )
inline

Perform a blocking wait on the timer.

This function is used to wait for the timer to expire. This function blocks and does not return until the timer has expired.

Exceptions
asio::system_errorThrown on failure.

◆ wait() [2/2]

template<typename Clock , typename WaitTraits , typename Executor >
void asio::basic_waitable_timer< Clock, WaitTraits, Executor >::wait ( asio::error_code ec)
inline

Perform a blocking wait on the timer.

This function is used to wait for the timer to expire. This function blocks and does not return until the timer has expired.

Parameters
ecSet to indicate what error occurred, if any.

The documentation for this class was generated from the following file: