5
// Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
7
// Distributed under the Boost Software License, Version 1.0. (See accompanying
8
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11
#ifndef ASIO_STRAND_HPP
12
#define ASIO_STRAND_HPP
14
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
16
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18
#include "asio/detail/push_options.hpp"
20
#include "asio/io_service.hpp"
21
#include "asio/detail/strand_service.hpp"
22
#include "asio/detail/wrapped_handler.hpp"
26
/// Provides serialised handler execution.
28
* The io_service::strand class provides the ability to post and dispatch
29
* handlers with the guarantee that none of those handlers will execute
33
* @e Distinct @e objects: Safe.@n
34
* @e Shared @e objects: Safe.
39
class io_service::strand
44
* Constructs the strand.
46
* @param io_service The io_service object that the strand will use to
47
* dispatch handlers that are ready to be run.
49
explicit strand(asio::io_service& io_service)
50
: service_(asio::use_service<
51
asio::detail::strand_service>(io_service))
53
service_.construct(impl_);
60
* Handlers posted through the strand that have not yet been invoked will
61
* still be dispatched in a way that meets the guarantee of non-concurrency.
65
service_.destroy(impl_);
68
/// (Deprecated: use get_io_service().) Get the io_service associated with
71
* This function may be used to obtain the io_service object that the strand
72
* uses to dispatch handlers for asynchronous operations.
74
* @return A reference to the io_service object that the strand will use to
75
* dispatch handlers. Ownership is not transferred to the caller.
77
asio::io_service& io_service()
79
return service_.get_io_service();
82
/// Get the io_service associated with the strand.
84
* This function may be used to obtain the io_service object that the strand
85
* uses to dispatch handlers for asynchronous operations.
87
* @return A reference to the io_service object that the strand will use to
88
* dispatch handlers. Ownership is not transferred to the caller.
90
asio::io_service& get_io_service()
92
return service_.get_io_service();
95
/// Request the strand to invoke the given handler.
97
* This function is used to ask the strand to execute the given handler.
99
* The strand object guarantees that handlers posted or dispatched through
100
* the strand will not be executed concurrently. The handler may be executed
101
* inside this function if the guarantee can be met. If this function is
102
* called from within a handler that was posted or dispatched through the same
103
* strand, then the new handler will be executed immediately.
105
* The strand's guarantee is in addition to the guarantee provided by the
106
* underlying io_service. The io_service guarantees that the handler will only
107
* be called in a thread in which the io_service's run member function is
108
* currently being invoked.
110
* @param handler The handler to be called. The strand will make a copy of the
111
* handler object as required. The function signature of the handler must be:
112
* @code void handler(); @endcode
114
template <typename Handler>
115
void dispatch(Handler handler)
117
service_.dispatch(impl_, handler);
120
/// Request the strand to invoke the given handler and return
123
* This function is used to ask the strand to execute the given handler, but
124
* without allowing the strand to call the handler from inside this function.
126
* The strand object guarantees that handlers posted or dispatched through
127
* the strand will not be executed concurrently. The strand's guarantee is in
128
* addition to the guarantee provided by the underlying io_service. The
129
* io_service guarantees that the handler will only be called in a thread in
130
* which the io_service's run member function is currently being invoked.
132
* @param handler The handler to be called. The strand will make a copy of the
133
* handler object as required. The function signature of the handler must be:
134
* @code void handler(); @endcode
136
template <typename Handler>
137
void post(Handler handler)
139
service_.post(impl_, handler);
142
/// Create a new handler that automatically dispatches the wrapped handler
145
* This function is used to create a new handler function object that, when
146
* invoked, will automatically pass the wrapped handler to the strand's
149
* @param handler The handler to be wrapped. The strand will make a copy of
150
* the handler object as required. The function signature of the handler must
151
* be: @code void handler(A1 a1, ... An an); @endcode
153
* @return A function object that, when invoked, passes the wrapped handler to
154
* the strand's dispatch function. Given a function object with the signature:
155
* @code R f(A1 a1, ... An an); @endcode
156
* If this function object is passed to the wrap function like so:
157
* @code strand.wrap(f); @endcode
158
* then the return value is a function object with the signature
159
* @code void g(A1 a1, ... An an); @endcode
160
* that, when invoked, executes code equivalent to:
161
* @code strand.dispatch(boost::bind(f, a1, ... an)); @endcode
163
template <typename Handler>
164
#if defined(GENERATING_DOCUMENTATION)
167
detail::wrapped_handler<strand, Handler>
169
wrap(Handler handler)
171
return detail::wrapped_handler<io_service::strand, Handler>(*this, handler);
175
asio::detail::strand_service& service_;
176
asio::detail::strand_service::implementation_type impl_;
179
/// Typedef for backwards compatibility.
180
typedef asio::io_service::strand strand;
184
#include "asio/detail/pop_options.hpp"
186
#endif // ASIO_STRAND_HPP