asio::async_write 和链

2023-07-19C/C++开发问题
2

本文介绍了asio::async_write 和链的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

asio::async_write(m_socket, asio::buffer(buf, bytes),
                custom_alloc(m_strand.wrap(custom_alloc(_OnSend))));

这段代码是否保证async_write内部的所有异步操作处理程序(调用async_write_some)都通过strand调用?(或者它只是为了 my_handler?)

Does this code guarantee that all asynchronous operation handlers(calls to async_write_some) inside async_write are called through strand? (or it's just for my_handler?)

推荐答案

使用以下代码:

asio::async_write(stream, ..., custom_alloc(m_strand.wrap(...)));

对于这个组合操作,如果以下所有条件都为真,所有对 stream.async_write_some() 的调用都将在 m_strand 内调用:

For this composed operation, all calls to stream.async_write_some() will be invoked within m_strand if all of the following conditions are true:

  • 发起的 async_write(...) 调用在 m_strand() 中运行:

assert(m_strand.running_in_this_thread());
asio::async_write(stream, ..., custom_alloc(m_strand.wrap(...)));

  • custom_alloc 的返回类型是:

    • strand::wrap()

    template <typename Handler> 
    Handler custom_alloc(Handler) { ... }
    

  • 一个自定义处理程序,用于适当链接 asio_handler_invoke():

    template <class Handler>
    class custom_handler
    {
    public:
      custom_handler(Handler handler)
        : handler_(handler)
      {}
    
      template <class... Args>
      void operator()(Args&&... args)
      {
        handler_(std::forward<Args>(args)...);
      }
    
      template <typename Function>
      friend void asio_handler_invoke(
        Function intermediate_handler,
        custom_handler* my_handler)
      {
        // Support chaining custom strategies incase the wrapped handler
        // has a custom strategy of its own.
        using boost::asio::asio_handler_invoke;
        asio_handler_invoke(intermediate_handler, &my_handler->handler_);
      }
    
    private:
      Handler handler_;
    };
    
    template <typename Handler>
    custom_handler<Handler> custom_alloc(Handler handler)
    {
      return {handler};
    }
    

  • 请参阅此答案以了解有关链的更多详细信息,以及this 回答有关 asio_handler_invoke 的详细信息.

    See this answer for more details on strands, and this answer for details on asio_handler_invoke.

    这篇关于asio::async_write 和链的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

    The End

    相关推荐

    无法访问 C++ std::set 中对象的非常量成员函数
    Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
    2024-08-14 C/C++开发问题
    17

    从 lambda 构造 std::function 参数
    Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
    2024-08-14 C/C++开发问题
    25

    STL BigInt 类实现
    STL BigInt class implementation(STL BigInt 类实现)...
    2024-08-14 C/C++开发问题
    3

    使用 std::atomic 和 std::condition_variable 同步不可靠
    Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
    2024-08-14 C/C++开发问题
    17

    在 STL 中将列表元素移动到末尾
    Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
    2024-08-14 C/C++开发问题
    9

    为什么禁止对存储在 STL 容器中的类重载 operator&amp;()?
    Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
    2024-08-14 C/C++开发问题
    6