0

Here is my code:

void client_connection::serve()
{
    asio::async_read(this->socket_, asio::buffer(&buffer_, buffer_.size()),

        // predicate/condition (do I wrap this?)
        std::bind(&client_connection::handle_read_predicate, this->shared_from_this(), std::placeholders::_1, std::placeholders::_2),

        // handler
        this->strand_.wrap(std::bind(&client_connection::handle_read, this->shared_from_this(), std::placeholders::_1, std::placeholders::_2)));
}

std::size_t client_connection::handle_read_predicate(const asio::error_code& error, std::size_t bytes_)
{
    // useless flawed function, for now

    // std::cout << "test: reached predicate, " << bytes_ << std::endl;

    return 0;
}

void client_connection::handle_read(const asio::error_code& error_, std::size_t bytes_)
{
    // useless flawed function, for now

    if (error_) return;

    this->serve();
}

My question is whether it would be correct usage of asio::io_service::strand to wrap the predicate/condition handler with the same strand_ object; if so, why, and if not, please explain.

kvanbere
  • 3,289
  • 3
  • 27
  • 52

1 Answers1

3

There is no need to wrap it in the strand.

Per the strand documented, for composed operations, such as the async_read free function, all intermediate handlers are invoked within the handler's strand. A side-effect of this is that all intermediate invocations of the CompletionCondition are also invoked from within the strand.

However, make sure to dispatch the initial invocation of client_connection::serve() within a strand when starting the asynchronous loop, as the initial CompletionCondition and asynchronous read socket operation occur within the context of the caller. For example, in the following illustration, all calls to socket.async_read(), client_connection::handle_read_predicate(), and client_connection::handle_read() will occur within the strand:

void client_connection::start()
{
  strand_.dispatch(std::bind(&client_connection::serve,
                             shared_from_this())) --------.
}                                                         |
    .-----------------------------------------------------'
    |  .--------------------------------------------------.
    V  V                                                  |
void client_connection::serve()                           |
{                                                         |
  async_read(socket_, buffer,                             |
    std::bind(&client_connection::handle_read_predicate,  |
              this),                                      |
    strand_.wrap(                                         |
      std::bind(&client_connection::handle_read,          |
                shared_from_this())); --.                 |
}                                       |                 |
    .-----------------------------------'                 |
    V                                                     |
void client_connection::handle_read(...)                  |
{                                                         |
  if (error) return;                                      |
  serve();  ----------------------------------------------'
}
Tanner Sansbury
  • 51,153
  • 9
  • 112
  • 169
  • So, if I understand correctly, I could remove the strand_.wrap from the example above and change the first strand_.dispatch to a strand_.wrap with no issues? – kvanbere Apr 08 '13 at 23:12
  • 1
    @kvanberendonck: No. `strand::wrap()` only creates a handler, neither the provided handler nor returned handler are dispatched or posted to the `io_service`. Replacing the `strand_.dispatch` would result in a no-op. The `strand_.dispatch` serializes the execution of the first `socket_.async_read()` and `handle_read_predicate`; while `strand_.wrap()` serializes all other `socket_.async_read()` calls, `handle_read`, and `handle_read_predicate` in the `server()` -> `handle_read()` asynchronous chain. See [this](http://stackoverflow.com/a/12801042/1053968) answer for more details. – Tanner Sansbury Apr 09 '13 at 12:21