Boost  v1.57.0
doxygen for www.boost.org
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
boost::asio::io_service::strand Class Reference

Provides serialised handler execution. More...

#include <strand.hpp>

Public Member Functions

 strand (boost::asio::io_service &io_service)
 Constructor. More...
 
 ~strand ()
 Destructor. More...
 
boost::asio::io_serviceget_io_service ()
 Get the io_service associated with the strand. More...
 
template<typename CompletionHandler >
 dispatch (BOOST_ASIO_MOVE_ARG(CompletionHandler) handler)
 Request the strand to invoke the given handler. More...
 
template<typename CompletionHandler >
 post (BOOST_ASIO_MOVE_ARG(CompletionHandler) handler)
 Request the strand to invoke the given handler and return immediately. More...
 
template<typename Handler >
detail::wrapped_handler
< strand, Handler,
detail::is_continuation_if_running > 
wrap (Handler handler)
 Create a new handler that automatically dispatches the wrapped handler on the strand. More...
 
bool running_in_this_thread () const
 Determine whether the strand is running in the current thread. More...
 

Detailed Description

Provides serialised handler execution.

The io_service::strand class provides the ability to post and dispatch handlers with the guarantee that none of those handlers will execute concurrently.

Order of handler invocation
Given:
  • a strand object s
  • an object a meeting completion handler requirements
  • an object a1 which is an arbitrary copy of a made by the implementation
  • an object b meeting completion handler requirements
  • an object b1 which is an arbitrary copy of b made by the implementation

if any of the following conditions are true:

  • s.post(a) happens-before s.post(b)
  • s.post(a) happens-before s.dispatch(b), where the latter is performed outside the strand
  • s.dispatch(a) happens-before s.post(b), where the former is performed outside the strand
  • s.dispatch(a) happens-before s.dispatch(b), where both are performed outside the strand

then asio_handler_invoke(a1, &a1) happens-before asio_handler_invoke(b1, &b1).

Note that in the following case:

async_op_1(..., s.wrap(a));
async_op_2(..., s.wrap(b));

the completion of the first async operation will perform s.dispatch(a), and the second will perform s.dispatch(b), but the order in which those are performed is unspecified. That is, you cannot state whether one happens-before the other. Therefore none of the above conditions are met and no ordering guarantee is made.

Note
The implementation makes no guarantee that handlers posted or dispatched through different strand objects will be invoked concurrently.
Thread Safety
Distinct objects: Safe.
Shared objects: Safe.
Concepts:
Dispatcher.

Constructor & Destructor Documentation

boost::asio::io_service::strand::strand ( boost::asio::io_service io_service)
inlineexplicit

Constructor.

Constructs the strand.

Parameters
io_serviceThe io_service object that the strand will use to dispatch handlers that are ready to be run.
boost::asio::io_service::strand::~strand ( )
inline

Destructor.

Destroys a strand.

Handlers posted through the strand that have not yet been invoked will still be dispatched in a way that meets the guarantee of non-concurrency.

Member Function Documentation

template<typename CompletionHandler >
boost::asio::io_service::strand::dispatch ( BOOST_ASIO_MOVE_ARG(CompletionHandler)  handler)
inline

Request the strand to invoke the given handler.

This function is used to ask the strand to execute the given handler.

The strand object guarantees that handlers posted or dispatched through the strand will not be executed concurrently. The handler may be executed inside this function if the guarantee can be met. If this function is called from within a handler that was posted or dispatched through the same strand, then the new handler will be executed immediately.

The strand's guarantee is in addition to the guarantee provided by the underlying io_service. The io_service guarantees that the handler will only be called in a thread in which the io_service's run member function is currently being invoked.

Parameters
handlerThe handler to be called. The strand will make a copy of the handler object as required. The function signature of the handler must be:
void handler();

References boost::asio::detail::async_result_init< Handler, Signature >::handler, boost::unit_test::framework::init(), boost::asio::detail::async_result_init< Handler, Signature >::result, and boost::detail::void.

boost::asio::io_service& boost::asio::io_service::strand::get_io_service ( )
inline

Get the io_service associated with the strand.

This function may be used to obtain the io_service object that the strand uses to dispatch handlers for asynchronous operations.

Returns
A reference to the io_service object that the strand will use to dispatch handlers. Ownership is not transferred to the caller.
template<typename CompletionHandler >
boost::asio::io_service::strand::post ( BOOST_ASIO_MOVE_ARG(CompletionHandler)  handler)
inline

Request the strand to invoke the given handler and return immediately.

This function is used to ask the strand to execute the given handler, but without allowing the strand to call the handler from inside this function.

The strand object guarantees that handlers posted or dispatched through the strand will not be executed concurrently. The strand's guarantee is in addition to the guarantee provided by the underlying io_service. The io_service guarantees that the handler will only be called in a thread in which the io_service's run member function is currently being invoked.

Parameters
handlerThe handler to be called. The strand will make a copy of the handler object as required. The function signature of the handler must be:
void handler();

References boost::asio::detail::async_result_init< Handler, Signature >::handler, boost::unit_test::framework::init(), boost::asio::detail::async_result_init< Handler, Signature >::result, and boost::detail::void.

bool boost::asio::io_service::strand::running_in_this_thread ( ) const
inline

Determine whether the strand is running in the current thread.

Returns
true if the current thread is executing a handler that was submitted to the strand using post(), dispatch() or wrap(). Otherwise returns false.
template<typename Handler >
detail::wrapped_handler<strand, Handler, detail::is_continuation_if_running> boost::asio::io_service::strand::wrap ( Handler  handler)
inline

Create a new handler that automatically dispatches the wrapped handler on the strand.

This function is used to create a new handler function object that, when invoked, will automatically pass the wrapped handler to the strand's dispatch function.

Parameters
handlerThe handler to be wrapped. The strand will make a copy of the handler object as required. The function signature of the handler must be:
void handler(A1 a1, ... An an);
Returns
A function object that, when invoked, passes the wrapped handler to the strand's dispatch function. Given a function object with the signature:
R f(A1 a1, ... An an);
If this function object is passed to the wrap function like so:
strand.wrap(f);
then the return value is a function object with the signature
void g(A1 a1, ... An an);
that, when invoked, executes code equivalent to:
strand.dispatch(boost::bind(f, a1, ... an));

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