首页 > 解决方案 > 提升 asio io_service 与 io_context

问题描述

我正在使用带有 c++ 的 boost asio 库。我发现 io_service 和 io_context 有相似之处。例如两者都有方法运行和其他。有人可以详细说明这两个类之间的区别(如用法、概念、结构差异等)

标签: c++boostboost-asio

解决方案


你应该使用io_context它,它取代了io_service.

根据提升问题#110

io_service已弃用。是的,你应该使用io_context. 请注意,“旧”API 也已被弃用(例如io_service.post(),您应该使用post(io_context, handler))。

. . .

io_service-> io_context
io_service.post()-> io_context.get_executor().post()
io_service.dispatch()->io_context.get_executor().dispatch()

io_service::strand->strand<io_context::executor_type>

组合操作自定义钩子也发生了变化 - 现在只有 2 个 -boost::asio::associated_allocatorboost::asio::associated_executor,默认查找 组合操作函数对象的 成员get_allocator()、 g et_executor()、 。T::allocator_typeT::executor_type

这不是一个完整的列表。

这些更改与Networking TS 兼容性有关。

似乎已在Boost 1.66中添加。


推荐阅读