我这么大个io_service去哪里了

最近稍微闲了下来,开始debug Boost.Asio 析构时崩溃的问题。在目前代码中,使用到了deadline_timer作定时器。

由于早期仅用简单的定时器,也就没多了解io_service的用法,简单地在每个短周期业务实例中各开了一组threadio_service来挂deadline_timer。后来出现了一个问题,运行时发现在析构业务实例时delete io_service偶尔会崩溃(具体定位过程就不在这边展开了),就去看了源代码和reference。

想着这么大的库api应该不会有太大的变化,点开boost最新版(1.71)reference,咦 O__O 我这么大个io_service去哪里了?

image-20191030105005556

Implemented interface changes to reflect the Networking TS (N4656).

  • See the list of new interfaces and, where applicable, the corresponding old interfaces that have been superseded.
  • The service template parameters, and the corresponding classes, are disabled by default. For example, instead of basic_socket we now have simply basic_socket. The old interface can be enabled by defining the BOOST_ASIO_ENABLE_OLD_SERVICES macro.

为了迎合C++ Networking TS的标准将接口改了,但是旧的接口依然还在支持。但一些service模板和相应的类默认不支持老版本,不过可以打开BOOST_ASIO_ENABLE_OLD_SERVICES宏来支持老版本service。

以下仅列了一些我感兴趣的修改,完整版可以看上文的list链接。

New interfaceOld interfaceNotes
io_contextio_serviceThe name io_service is retained as a typedef.
dispatchio_service::dispatchThe dispatch free function can be used to submit functions to any Executor or ExecutionContext.
postio_service::postThe dispatch free function can be used to submit functions to any Executor or ExecutionContext.
deferio_service::post when the asio_handler_is_continuation hook returns trueThe defer free function can be used to submit functions to any Executor or ExecutionContext.
io_context::pollio_service::poll overload that takes error_code&The error_code overload is not required.
io_context::poll_oneio_service::poll_one overload that takes error_code&The error_code overload is not required.
io_context::runio_service::run overload that takes error_code&The error_code overload is not required.
io_context::run_oneio_service::run_one overload that takes error_code&The error_code overload is not required.
io_context::run_for, io_context::run_until, io_context::run_one_for, and io_context::run_one_untilThese functions add the ability to run an io_context for a limited time.
io_context::restartio_service::reset
execution_context, execution_context::service, and execution_context::idio_service, io_service::service, and io_service::idThe service-related functionality has been moved to the execution_context base class. This may also be used as a base for creating custom execution contexts.
make_serviceadd_service
strandio_service::strandThis template works with any valid executor, and is itself a valid executor.
executor_work_guard and make_work_guardio_service::workWork tracking is now covered by the Executor requirements. These templates work with any valid executor.