c++ - 澄清在 boost::strand 中执行的内容

标签 c++ boost boost-asio

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

下面的类是一个 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,当从传递给 postlambda 中调用时,正在 中运行>链

3: async_writestrand 中被调用。如果 async_write 尚未完成,则 strand 将不会调度下一个处理程序。

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

5 Write 是从 async_write 完成处理程序中调用的,并且中运行>链

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

最佳答案

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.

不正确。当传递给 strand 的 lambda“返回”时,下一个排队的 strand 工作将运行。

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.

再次不正确,当传递给 strand 的 lambda“返回”时,strand 工作的下一个排队位将运行。

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

正确。

如果您希望 async_write 完成处理程序在 strand 中运行(您最有可能这样做是因为对 _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( );
        }
    } ));
}

关于c++ - 澄清在 boost::strand 中执行的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51911441/

相关文章:

c++ - 在 Visual C++ 2010 上哪个更快 - std::shared_ptr 或 boost::shared_ptr?

c++ - 从作为分离线程运行的 boost::asio::io_service::work 捕获异常

c++ - boost asio udp async_read_from buffersize

c++ - 对 boost asio 完成处理程序的右值引用

c++:将函数作为参数传递给另一个函数

c++ - 添加 boost 库作为 Bazel 依赖项 c++

c++ - 为 STD vector<bool> 模板特化预留容量

c++ - 当在多个线程中使用 read_json 而不链接 boost.thread 库时,我们是否可以始终使用 BOOST_SPIRIT_THREADSAFE 标志?

c++ - 一次加载大块 csv 而不是使用 getline 的方法?

c++ - 为什么 c++11 正则表达式(libc++ 实现)这么慢?