我这么大个io_service去哪里了
我这么大个io_service去哪里了
[TOC]
最近稍微闲了下来,开始debug Boost.Asio
析构时崩溃的问题。在目前代码中,使用到了deadline_timer
作定时器。
由于早期仅用简单的定时器,也就没多了解io_service
的用法,简单地在每个短周期业务实例中各开了一组thread
和io_service
来挂deadline_timer
。后来出现了一个问题,运行时发现在析构业务实例时delete io_service
偶尔会崩溃(具体定位过程就不在这边展开了),就去看了源代码和reference。
想着这么大的库api应该不会有太大的变化,点开boost最新版(1.71)reference,咦 O__O 我这么大个io_service
去哪里了?
Legend 1:Boost 1.71 Asio Reference
接下来从1.71版本往回翻,在1.66更新信息里发现一句话:
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 simplybasic_socket
. The old interface can be enabled by defining theBOOST_ASIO_ENABLE_OLD_SERVICES
macro.
为了迎合C++ Networking TS的标准将接口改了,但是旧的接口依然还在支持。但一些service模板和相应的类默认不支持老版本,不过可以打开BOOST_ASIO_ENABLE_OLD_SERVICES
宏来支持老版本service。
以下仅列了一些我感兴趣的修改,完整版可以看上文的list链接。
New interface | Old interface | Notes |
---|---|---|
io_context |
io_service |
The name io_service is retained as a typedef. |
dispatch |
io_service::dispatch |
The dispatch free function can be used to submit functions to any
Executor or
ExecutionContext. |
post |
io_service::post |
The dispatch free function can be used to submit functions to any
Executor or
ExecutionContext. |
defer |
io_service::post when the
asio_handler_is_continuation hook returns true |
The defer free function can be used to submit functions to any
Executor or
ExecutionContext. |
io_context::poll |
io_service::poll overload that takes error_code& |
The error_code overload is not required. |
io_context::poll_one |
io_service::poll_one overload that takes error_code& |
The error_code overload is not required. |
io_context::run |
io_service::run overload that takes error_code& |
The error_code overload is not required. |
io_context::run_one |
io_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_until |
These functions add the ability to run an io_context for a limited time. |
|
io_context::restart |
io_service::reset |
|
execution_context ,
execution_context::service , and
execution_context::id |
io_service ,
io_service::service , and
io_service::id |
The 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_service |
add_service |
|
strand |
io_service::strand |
This template works with any valid executor, and is itself a valid executor. |
executor_work_guard and
make_work_guard |
io_service::work |
Work tracking is now covered by the Executor requirements. These templates work with any valid executor. |
Legend 2:Boost 1.71 Asio Interface Change List
在新接口表中我们可以看到`io_service`已经换成了`io_context`,且在1.66的[io_service](https://www.boost.org/doc/libs/1_66_0/doc/html/boost_asio/reference/io_service.html) reference中可以发现最上面就写了
reads