Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost asio io_service vs io_context

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)

like image 261
Vladimir Yanakiev Avatar asked Jan 15 '20 14:01

Vladimir Yanakiev


People also ask

What is Boost :: ASIO :: io_service?

Asio defines boost::asio::io_service , a single class for an I/O service object. Every program based on Boost. Asio uses an object of type boost::asio::io_service . This can also be a global variable. While there is only one class for an I/O service object, several classes for I/O objects exist.

What is boost Io_context?

The io_context class provides the core I/O functionality for users of the asynchronous I/O objects, including: boost::asio::ip::tcp::socket.

Is boost ASIO post thread safe?

Thread Safety In general, it is safe to make concurrent use of distinct objects, but unsafe to make concurrent use of a single object. However, types such as io_service provide a stronger guarantee that it is safe to use a single object concurrently.

What is ASIO boost?

Boost. Asio is a cross-platform C++ library for network and low-level I/O programming that provides developers with a consistent asynchronous model using a modern C++ approach. Overview. An overview of the features included in Boost. Asio, plus rationale and design information.


1 Answers

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.

like image 89
rustyx Avatar answered Sep 21 '22 10:09

rustyx