Boost ASIO: recovering from handler exceptions

亡梦爱人 提交于 2019-12-11 06:43:41

问题


If an ASIO callback throws an error is it safe to resume the async processing?

In short, does the following code have any merit?

void runAsioLoop()
{
    boost::asio::io_service::work work(this->m_ioService);
    boost::system::error_code unused;

    while (m_running) {
        try {
            this->m_ioService.run(unused);
            this->m_ioService.reset();
        } catch (...) {
            std::cerr << "*** An error happened\n";
        }
    }
}

回答1:


It should work, but the better idiom is:

for (;;) {
    try {
        svc.run();
        break; // exited normally
    } catch (std::exception const &e) {
        logger.log(LOG_ERR) << "[eventloop] An unexpected error occurred running a task: " << e.what();
    } catch (...) {
        logger.log(LOG_ERR) << "[eventloop] An unexpected error occurred running a task";
    }
}

Here's the documentation for it: http://www.boost.org/doc/libs/1_65_1/doc/html/boost_asio/reference/io_service.html#boost_asio.reference.io_service.effect_of_exceptions_thrown_from_handlers



来源:https://stackoverflow.com/questions/46934078/boost-asio-recovering-from-handler-exceptions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!