30

I am using boost asio library with c++. I found that io_service and io_context have similarities. for example both have method run and others. Could someone please detail differences between these two classes( like usage, conceptual idea, structural difference et.c)

rustyx
  • 80,671
  • 25
  • 200
  • 267
Vladimir Yanakiev
  • 1,240
  • 1
  • 16
  • 25

1 Answers1

36

You should use io_context, it replaces io_service.

According to boost issue #110:

io_service is deprecated. Yes, you should use io_context. Beware that the "old" API is deprecated as well (e.g. io_service.post(), you should use 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>

there were also changes to composed operation customization hooks - there are only 2 now - boost::asio::associated_allocator and boost::asio::associated_executor, which default to looking for get_allocator(), get_executor(), T::allocator_type, T::executor_type members of the composed operation function object.

This is not a complete list.

These changes are related to Networking TS compatibility.

Seems to have been added in Boost 1.66.

Adam
  • 2,726
  • 1
  • 9
  • 22
rustyx
  • 80,671
  • 25
  • 200
  • 267
  • 2
    Some documentation seems to be out of date as in the examples of `1.71` they are still using `io_service`: https://www.boost.org/doc/libs/1_71_0/doc/html/boost_process/tutorial.html – juzzlin Jul 02 '21 at 09:38