Correct usage of asio::io_service::strand?

前端 未结 1 397
陌清茗
陌清茗 2021-01-26 04:16

Here is my code:

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

        // predicate/condi         


        
相关标签:
1条回答
  • 2021-01-26 05:02

    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();  ----------------------------------------------'
    }
    
    0 讨论(0)
提交回复
热议问题