本文介绍了正确使用asio :: io_service :: strand?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

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();
}

我的问题是是否正确使用asio :: io_service strand用相同的strand_对象​​包装谓词/条件句柄;

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.

推荐答案

没有必要把它包裹在链中。

There is no need to wrap it in the strand.

按,对于组合操作,例如 async_read free函数,所有中间处理程序都在处理程序的strand中调用。这种情况的副作用是, CompletionCondition 的所有中间调用也都从内部调用。

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.

但是,在启动异步循环时,请确保在一个strand中调用 client_connection :: serve()的初始调用,因为初始 CompletionCondition 和异步读套接字操作发生在调用者的上下文中。例如,在下面的例子中,所有调用 socket.async_read() client_connection :: handle_read_predicate() client_connection :: handle_read()将出现在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();  ----------------------------------------------'
}

这篇关于正确使用asio :: io_service :: strand?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 14:33