澄清 boost::strand 中正在执行的内容

clarification on whats being executed in a boost::strand

我有一个问题,关于 strand 中的 运行 是什么,什么不是。我已经阅读了关于 SO 的帖子以及 strand 的文档,但我想确保我已经正确理解了与下面代码相关的内容。

下面的 class 是一个 tcp 客户端,它异步发送东西给关心的人。

我还想提一下 io_service::run 已被多次调用 threads

int main( )
{ 
    /* Other stuff */
    client.WriteAsync( buffer1 ); 
    client.WriteAsync( buffer2 );
}


/* TcpClient class */

void TcpClient::WriteAsync( std::shared_ptr<boost::asio::streambuf> buffer )
{
    // 1
    _strand.post( [ this, buffer ]( ) 
    {
        _outbuffer.push( buffer );
        if ( _outbuffer.size( ) > 1 ) return;
        // 2
        Write( );
    } );
}

void TcpClient::Write( )
{       
    // 3
    boost::asio::async_write( _socket,
        *_outbuffer.front( ),
        // 4
        [ this ]( boost::system::error_code const& error,
            size_t const bytesTransferred )
    {               
        _outbuffer.pop( );          
        if ( error )
        {
            std::cout << "Error writing: " << error.message( ) << std::endl;
        }
        if ( !_outbuffer.empty( ) )
        {
            // 5
            Write( );
        }
    } );
}

1: _strand.post 被调用,如果当前没有任何东西 运行 则 strand 将调度传入的处理程序。在这种情况下,传递给 strand 的 lambda 将在 strand 中执行。如果已经有工作正在完成,处理程序将排队。

2: Write,当从 lambda 内部调用时传递给 post 是 运行 在 strand

3: async_writestrand内调用。如果 async_write 尚未完成,strand 将不会派遣下一个处理程序。

4: async_write 完成处理程序是 而不是 运行 在 strand 中。当调用 async_write 完成处理程序时,strand 将从其队列中弹出下一个工作单元并分派它。

5: Writeasync_write 完成处理程序中调用并且 不是 运行 在 strand

我想知道我上面的说法是否正确

1: _strand.post is called, if there is nothing currently running the strand will dispatch the passed in handler. In this case the lambda being passed to the strand will execute in the strand. If there is already work being done, the handler will be queued.

正确。

2: Write, when called from within the lambda passed to post is running in the strand

正确。

3: async_write is called within the strand. If async_write has not completed the strand will not have dispatched the next handler.

不正确。当 lambda 传递给链 "returns" 时,链工作的下一个排队位是 运行.

4: The async_write completion handler is not running in the strand.

正确

When the async_write completion handler is invoked the strand will pop the next unit of work off of its queue and dispatch it.

再次不正确,当 lambda 传递给链 "returns" 时,链工作的下一个排队位是 运行。

5: Write is called from within the async_write completion handler and is not running in the strand

正确。

如果您希望 async_write 完成处理程序在链中 运行(您很可能这样做是因为对 _outbuffer 的共享访问),您可以使用 bind_executor

例如

void TcpClient::Write( )
{       
    boost::asio::async_write( _socket,
        *_outbuffer.front( ),
        // here
        boost::asio::bind_executor(_strand, [ this ]( boost::system::error_code const& error, size_t const bytesTransferred )
    {               
        _outbuffer.pop( );          
        if ( error )
        {
            std::cout << "Error writing: " << error.message( ) << std::endl;
        }
        if ( !_outbuffer.empty( ) )
        {
            // 5
            Write( );
        }
    } ));
}