【问题标题】:boost asio custom allocator handler io service post compile errorsboost asio 自定义分配器处理程序 io 服务后编译错误
【发布时间】:2015-02-24 15:24:46
【问题描述】:

我有如下工作的 io_service 后调用:

  _io_service.post(std::tr1::bind(&BlitzLogger::push,this,
                                    std::tr1::bind(&BlitzLogger::stringer<typename boost::decay<T const &>::type,
                                                   typename boost::decay<Args const &>::type ...>, this,
                                                   t, args ...)));

我怀疑绑定调用有一些可以消除的开销,所以我开始为处理程序布置自定义分配器,如

所述
 http://www.boost.org/doc/libs/1_50_0/doc/html/boost_asio/example/allocation/server.cpp

接下来,我想做如下事情:

_io_service.post(
    makeCustomAllocHandler(_allocator1,std::tr1::bind(&BlitzLogger::push,this,
    makeCustomAllocHandler(_allocator2,std::tr1::bind(&BlitzLogger::stringer<typename boost::decay<T const &>::type,
                                                      typename boost::decay<Args const &>::type ...>,this,t,args ...)))));

上面这段代码抛出编译时错误(模板参数扣除/替换失败的主机), 但是,如果我删除 _io_service.post 调用,并将其限制为

    makeCustomAllocHandler(_allocator1,std::tr1::bind(&BlitzLogger::push,this,
    makeCustomAllocHandler(_allocator2,std::tr1::bind(&BlitzLogger::stringer<typename boost::decay<T const &>::type,
                                                      typename boost::decay<Args const &>::type ...>,this,t,args ...))));

然后代码编译得很好。

所以很明显问题是 makeCustomAllocHandler 的返回类型不符合 post 函数的模板处理程序参数。

为什么会发生这种情况,我该如何解决这个问题。

【问题讨论】:

  • 考虑使用分析器来识别瓶颈。自定义分配器处理程序不会消除调用std::bind() 或其返回的函子的开销,因为asio_handler_allocate() 挂钩仅用于控制临时处理程序的分配策略。为了最小化函子调用开销,需要编写特定的函子而不是包装std::bind函子。
  • @TannerSansbury 是的,性能数字似乎暗示了你在说什么

标签: c++ templates boost bind boost-asio


【解决方案1】:

为了与makeCustomAllocHandler 返回的CompletionHandler 对象兼容,应定义void operator()() 而不是ReadHandler 所需的void operator()(const boost::system::error_code&amp;, std::size_t),并在服务器示例中使用。

【讨论】:

  • 已经这样做了 void operator()(){ _handler(); }
  • 您的示例不包含任何方法定义和错误详细信息,因此很难猜测可能是什么错误。您至少可以声明 std::function&lt;void()&gt; 对象并尝试将您的 bindmakeCustomAllocHandler 替换为对该对象的引用,以查看错误何时消失。
  • 例如如果你有std::function&lt;void()&gt; f,那么_io_service.post(f) 应该编译,然后_io_service.post(makeCustomAllocHandler(_allocator1, f)) 也应该编译等等。
  • 嗯,你是对的,内部处理程序实际上返回一个字符串,所以我需要一个 std::string operator()() 。谢谢
猜你喜欢
  • 2014-02-08
  • 1970-01-01
  • 1970-01-01
  • 2013-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多