What exacty is io_context?
Asked Answered
B

1

23

I have been going throught the boost asio library in which most of the objects need the io_context object as argument to the constructor. I have read what io_context object,according to the documentation it states that it is

The io_context class provides the core I/O functionality for users of the asynchronous I/O objects

Which confuses me because isn't that what iostream does.I'm really sure i'm missing something,please help me clarify it and also i don't see much difference between I/O objects with the sockets other than fact that sockets can be used for exchanging data between two devices whereas I/O objects enable us to exchange data with our computer.I'm really confused at this point!

Boric answered 2/4, 2020 at 17:53 Comment(0)
I
28

io_context contains the state required to run the event-loop in the thread using platform-specific calls, such as select or epoll. Normally, there can be one (active) event loop per thread.

This component runs the event-loop invoking low-level platform-specific calls to acquire operating-system events such as socket readiness, timer, signal, idle and deliver them to user-registered callbacks in portable fashion.

It doesn't do any I/O, rather it invokes the callbacks when I/O can be done, e.g. network data has arrived and the socket is ready for read. The callbacks are expected to do their processing/non-blocking-I/O promptly and return in order to not block the event-loop in the thread.

Such a component is sometimes called an operating-system event demultiplexer because calls like select and epoll report file descriptor readiness, timeout and signal events by returning different values, which must be examined first (along with errno for signals) to distinguish what events it has reported. This examination of the return value to detect the event type followed by calling the corresponding event handler is demultiplexing of different event types reported by one syscall. These syscalls block the calling thread (to avoid burning CPU cycles / draining batteries) until one of these events happens, and that's why the caller has to demultiplex the return value to tell what kind of event is reported.

See Basic Boost.Asio Anatomy for more details.

Other popular event loops are libevent and libuv

The C10K problem is old but quite instructive on the subject of async I/O.


C++20 coroutines introduce a new programming model, which has benefits of being simpler to write and read and it mitigates callback hell inherent in non-blocking I/O code. See full details in Boost.Asio supports coroutines.

Async/coroutines I/O model is cheaper/easier to code for. It is still the same event-loop invoking the callbacks underneath, but now these callbacks do the coroutine state create/save/load/destroy to implement the async programming model for the user.

Imogeneimojean answered 2/4, 2020 at 18:1 Comment(5)
Can each process have only 1 io_context ? Is it possible to have multiple threads in a process and each thread running an io_context ?Introduce
@HariniSj boost.org/doc/libs/develop/doc/html/boost_asio/overview/core/…Imogeneimojean
@MaximEgorushkin Sorry, I have carefully read the document which is linked to, but I am still not so sure. As per the document, which says that:“In general, it is safe to make concurrent use of distinct objects.” So, it seems that there could be more than one io_context in a single process. More is at the next comment.Gwenore
@MaximEgorushkin I am confused again after I have read another document. As per this document, which says that:The program must ensure that the stream performs no other write operations (such as async_write, the stream's async_write_some function, or any other composed operations that perform writes) until this operation completes. It seems that two boost::ansy_write could not be could be called at the same time, so it seeems that it's useless to have more than one io_context in a single process.Gwenore
The link given in See Basic Boost.Asio Anatomy for more details. is broken. This is the correct link now: boost.org/doc/libs/1_76_0/doc/html/boost_asio/overview/core/… (Edit queue is full, so can't edit the post)Orebro

© 2022 - 2024 — McMap. All rights reserved.