为什么没有strand :: wrap()等效于strand :: post()?

时间:2013-04-26 16:05:38

标签: c++ boost boost-asio

定义strand::wrap()的行为,以便创建一个在调用时执行strand::dispatch()的仿函数。我最近遇到了一个执行以下序列的应用程序中的错误:

my_great_function(..., s.wrap(a), s.wrap(b));

应用程序保证s.wrap(a)之前调用s.wrap(b)创建的仿函数。但是,存在一种竞争条件,即第一个仿函数在strand之外被调用,因此推迟调用,而第二个仿函数在内部被调用并立即执行。这违反了应用程序在a之前b的排序假设,并导致了未定义的行为。

使用strand::post()代替strand::dispatch()是解决此问题的一种方法,但是使用strand.wrap()并没有简单的方法来实现这一点。我可以创建辅助函数来发布链,我想知道是否有更简单的方法?

1 个答案:

答案 0 :(得分:2)

纯粹猜测为什么strand.wrap的{​​{1}}等价物不存在:

  • 我无法找到任何人在功能请求中正式提出需要strand.post等效的案例。
  • 基于strand.wrap()最常见的用法,它可能以strand.wrap()的形式实现,以优化组合操作的中间处理程序。可以在满足完成条件后立即调用用户的完成处理程序,而不必为延迟调用发布完成处理程序。

最简单的解决方案可能是将strand.dispatch()my_great_functiona处理程序一起传递给b。如果my_great_function需要特定的处理程序调用顺序,那么让my_great_function保证排序而不是将onus传递给调用者似乎是可以接受的,这可能会忽略必要的顺序。另一方面,如果my_great_function是相当通用的,并且处理程序需要它们之间的特定调用顺序,那么考虑将处理程序一起传递到直接或间接暗示排序的结构中,例如std::tuple

虽然这些解决方案都没有提供通用的可重用解决方案,但它可能是最简单的解决方案。官方支持的解决方案是使用自定义处理程序类型,以及通过asio_handler_invoke提供ADL函数来计算操作的中间和完成处理程序。以下是一个完整的示例,主要基于detail/wrapped_handler.hpp

#include <iostream>

#include <boost/asio.hpp>

/// @brief Custom handler wrapper type that will post into its dispatcher.
template <typename Dispatcher,
          typename Handler>
class post_handler
{
public:
  typedef void result_type;

  post_handler(Dispatcher dispatcher, Handler handler)
    : dispatcher_(dispatcher),
      handler_(handler)
  {}

  void operator()()
  {
    dispatcher_.post(handler_);
  }

  template <typename Arg1>
  void operator()(Arg1 arg1)
  {
    dispatcher_.post(boost::bind(handler_, arg1));
  }

  template <typename Arg1, typename Arg2>
  void operator()(Arg1 arg1, Arg2 arg2)
  {
    dispatcher_.post(boost::bind(handler_, arg1, arg2));
  }

  Dispatcher dispatcher_;
  Handler handler_;
};

// Custom invocation hooks for post_handler.  These must be declared in 
// post_handler's associated namespace for proper resolution.

template <typename Function, typename Dispatcher, typename Handler>
inline void asio_handler_invoke(Function& function,
    post_handler<Dispatcher, Handler>* this_handler)
{
  this_handler->dispatcher_.post(
      boost::asio::detail::rewrapped_handler<Function, Handler>(
        function, this_handler->handler_));
}

template <typename Function, typename Dispatcher, typename Handler>
inline void asio_handler_invoke(const Function& function,
    post_handler<Dispatcher, Handler>* this_handler)
{
  this_handler->dispatcher_.post(
      boost::asio::detail::rewrapped_handler<Function, Handler>(
        function, this_handler->handler_));
}

/// @brief Factory function used to create handlers that post through the
///        dispatcher.
template <typename Dispatcher, typename Handler>
post_handler<Dispatcher, Handler>
wrap_post(Dispatcher dispatcher, Handler handler)
{
  return post_handler<Dispatcher, Handler>(dispatcher, handler);
}

/// @brief Convenience factory function used to wrap handlers created from
///        strand.wrap.
template <typename Dispatcher, typename Handler>
post_handler<Dispatcher, 
             boost::asio::detail::wrapped_handler<Dispatcher, Handler> >
wrap_post(boost::asio::detail::wrapped_handler<Dispatcher, Handler> handler)
{
  return wrap_post(handler.dispatcher_, handler);
}

boost::asio::io_service io_service;
boost::asio::strand strand(io_service);
boost::asio::deadline_timer timer(io_service);

void a() { std::cout << "a" << std::endl; }
void b() { std::cout << "b" << std::endl; }
void c() { std::cout << "c" << std::endl; }
void d() { std::cout << "d" << std::endl; }
void noop() {}

void my_great_function()
{
  std::cout << "++my_great_function++" << std::endl;
  // Standard dispatch.
  strand.dispatch(&a);

  // Direct wrapping.
  wrap_post(strand, &b)();

  // Convenience wrapping.
  wrap_post(strand.wrap(&c))();

  // ADL hooks.
  timer.async_wait(wrap_post(strand.wrap(boost::bind(&d))));
  timer.cancel();
  std::cout << "--my_great_function--" << std::endl;
}

int main()
{
  // Execute my_great_function not within a strand.  The noop
  // is used to force handler invocation within strand.
  io_service.post(&my_great_function);
  strand.post(&noop);
  io_service.run();
  io_service.reset();

  // Execute my_great_function within a strand.
  std::cout << std::endl;
  io_service.post(strand.wrap(&my_great_function));
  strand.post(&noop);
  io_service.run();
}

产生以下输出:

++my_great_function++
--my_great_function--
a
b
c
d

++my_great_function++
a
--my_great_function--
b
c
d

依赖于实现细节的稍微简单的解决方案是调整detail::wrapped_handler的{​​{1}}类型参数。这种方法允许Dispatcher具有适应的wrapped_handler类型,以便在Boost.Asio的其余部分中透明地使用。

Dispatcher

两个/// @brief Class used to adapter the wrapped_handler's Dispatcher type /// requirement to post handlers instead of dispatching handlers. template <typename Dispatcher> struct post_adapter { post_adapter(Dispatcher& dispatcher) : dispatcher_(dispatcher) {} template <typename Handler> void dispatch(const Handler& handler) { dispatcher_.post(handler); } Dispatcher dispatcher_; }; /// @brief Factory function used to create handlers that post through an /// adapted dispatcher. template <typename Dispatcher, typename Handler> boost::asio::detail::wrapped_handler<post_adapter<Dispatcher>, Handler> wrap_post(Dispatcher& dispatcher, Handler handler) { typedef post_adapter<Dispatcher> adapter_type; return boost::asio::detail::wrapped_handler< adapter_type, Handler>(adapter_type(dispatcher), handler); } /// @brief Convenience factory function used to wrap handlers created from /// strand.wrap. template <typename Dispatcher, typename Handler> boost::asio::detail::wrapped_handler< post_adapter<Dispatcher>, boost::asio::detail::wrapped_handler<Dispatcher, Handler> > wrap_post(boost::asio::detail::wrapped_handler<Dispatcher, Handler> handler) { return wrap_post(handler.dispatcher_, handler); } 解决方案可能会对定义的order of handler invocations引入一定程度的复杂性。