我想向我的 boost io_service 添加一个信号处理程序,允许应用程序在用户按下 Ctrl-C 时干净地关闭。这当然可以通过停止循环轻松完成,如下所示:
boost::asio::io_service service;
boost::asio::signal_set signals{ service, SIGINT, SIGTERM };
signals.async_wait(std::bind(&boost::asio::io_service::stop, &service));
这会正常停止循环,允许析构函数执行例行的清理行为。
问题是,一旦应用程序耗尽工作,它就不会停止,因为信号处理程序仍然注册了一个处理程序,因此 io_service 永远不会停止运行。
我还没有找到解决这个问题的干净方法。我当然可以自己进行信号处理,然后停止循环,但这违背了使用 boost(可移植性)的想法。
最佳答案
在下面的代码中,http_server
有一个“监听套接字”来接受多个连接。监听套接字不断运行async_accept
,因此io_service
永远不会停止工作。 http_server.shutdown()
函数关闭监听套接字和所有打开的连接,因此 io_service
没有更多工作并停止运行:
void handle_stop(ASIO_ERROR_CODE const&, // error,
int, // signal_number,
http_server_type& http_server)
{
std::cout << "Shutting down" << std::endl;
http_server.shutdown();
}
...
ASIO::io_service io_service;
http_server_type http_server(io_service);
...
// The signal set is used to register termination notifications
ASIO::signal_set signals_(io_service);
signals_.add(SIGINT);
signals_.add(SIGTERM);
#if defined(SIGQUIT)
signals_.add(SIGQUIT);
#endif // #if defined(SIGQUIT)
// register the handle_stop callback
signals_.async_wait([&http_server]
(ASIO_ERROR_CODE const& error, int signal_number)
{ handle_stop(error, signal_number, http_server); });
...
io_service.run();
std::cout << "io_service.run complete, shutdown successful" << std::endl;
此方法也适用于线程池,请参阅:thread_pool_http_server.cpp
关于c++ - Boost asio 处理程序不会保持 io_service 运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45675136/