Boost.Asio-不执行所有处理程序

时间:2019-01-14 22:06:07

标签: c++ asynchronous boost boost-asio

我正在尝试为boost::io_context创建一个适配器,该适配器将始终选择要执行的适配器中优先级最高的处理程序。我从the official example那里得到了启发,但是在一个处理程序在同一上下文中启动另一个异步操作的情况下,很快遇到了意外的行为。

这里是MCVE。我只修改了用户代码(在//---下面)来调用低优先级处理程序,然后我希望可以调用高优先级和中优先级处理程序。仅低优先级处理程序被调用。

#include <boost/asio.hpp>
#include <boost/function.hpp>
#include <iostream>
#include <queue>

class handler_priority_queue
{
public:
  void add(int priority, boost::function<void()> function)
  {
    handlers_.push(queued_handler(priority, function));
  }

  void execute_all()
  {
    while (!handlers_.empty())
    {
      queued_handler handler = handlers_.top();
      handler.execute();
      handlers_.pop();
    }
  }

  // A generic wrapper class for handlers to allow the invocation to be hooked.
  template <typename Handler>
  class wrapped_handler
  {
  public:
    wrapped_handler(handler_priority_queue& q, int p, Handler h)
      : queue_(q), priority_(p), handler_(h)
    {
    }

    void operator()()
    {
      handler_();
    }

    template <typename Arg1>
    void operator()(Arg1 arg1)
    {
      handler_(arg1);
    }

    template <typename Arg1, typename Arg2>
    void operator()(Arg1 arg1, Arg2 arg2)
    {
      handler_(arg1, arg2);
    }

  //private:
    handler_priority_queue& queue_;
    int priority_;
    Handler handler_;
  };

  template <typename Handler>
  wrapped_handler<Handler> wrap(int priority, Handler handler)
  {
    return wrapped_handler<Handler>(*this, priority, handler);
  }

private:
  class queued_handler
  {
  public:
    queued_handler(int p, boost::function<void()> f)
      : priority_(p), function_(f)
    {
    }

    void execute()
    {
      function_();
    }

    friend bool operator<(const queued_handler& a,
        const queued_handler& b)
    {
      return a.priority_ < b.priority_;
    }

  private:
    int priority_;
    boost::function<void()> function_;
  };

  std::priority_queue<queued_handler> handlers_;
};

// Custom invocation hook for wrapped handlers.
template <typename Function, typename Handler>
void asio_handler_invoke(Function f,
    handler_priority_queue::wrapped_handler<Handler>* h)
{
  h->queue_.add(h->priority_, f);
}

//----------------------------------------------------------------------

void high_priority_handler()
{
  std::cout << "High priority handler\n";
}

void middle_priority_handler()
{
  std::cout << "Middle priority handler\n";
}

void low_priority_handler(
  boost::asio::io_service& io_service,
  handler_priority_queue& pri_queue)
{
  std::cout << "Low priority handler\n";

  io_service.post(pri_queue.wrap(1, middle_priority_handler));
  io_service.post(pri_queue.wrap(2, high_priority_handler));
}

int main()
{
  boost::asio::io_service io_service;
  handler_priority_queue pri_queue;

  // Post a completion handler to be run immediately.
  io_service.post(pri_queue.wrap(
      0, std::bind(low_priority_handler,
                   std::ref(io_service), std::ref(pri_queue))));

  while (io_service.run_one())
  {
    // The custom invocation hook adds the handlers to the priority queue
    // rather than executing them from within the poll_one() call.
    while (io_service.poll_one())
      ;

    pri_queue.execute_all();
  }

  return 0;
}

如果我在io_service.restart()中的循环之后调用main,然后在该循​​环中复制粘贴,则其余的处理程序将按预期的顺序执行。调试时,我只能看到一个处理程序仅在asio_handler_invoke中排队。

为什么boost::io_context在第一个处理程序之后停止运行?我要的是可能的吗?

1 个答案:

答案 0 :(得分:2)

io_context停止是因为调用poll_one时没有任何就绪的处理程序可以运行。

[1]发布了第一个处理程序:

io_service.post(pri_queue.wrap(0, std::bind(low_priority_handler,
                   std::ref(io_service), std::ref(pri_queue))));

[2] while (io_service.run_one())

等待直到有一个就绪的处理程序可以运行

run_one()处理程序中的

[3]被执行。您已经定义了asio_handler_invoke(),它提供了一些策略来调用处理程序的函数(主体)。默认策略只是调用函数,在这种情况下,函数对象已排队到handler_priority_queue中,但是io_service's队列中没有要执行的处理程序。那么,何时执行low_priority_handler的主体(通过调用io_serviceio_service.post添加新的处理程序)?此函数从pri_queue.execute_all()(在<{1}} 之后调用的)执行,而不是在poll_one的调用期间执行。如果没有任何就绪的处理程序可以运行,poll_one()会将io_service.poll_one()标记为已停止。这是你的情况。您可以在io_service之后重设io_service,然后将调用所有处理程序。