Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the impact of calling io_service::run method twice

The following schema come from boost asio documentation:

enter image description here

I understand that if I call io_service::run method twice (in two separate threads), I will have two threads to deque events from the completion Event Queue via Asynchronous Event Demultiplexer am I right?

More precisely, my doubt is on the parrallelization achieve by multiple call of io_service::run method. For instance when dealing with socket, if for example I have two sockets bound on the same io_service object, each socket calling socket.async_read_some method, does it involved the 2 registered callbacks (via async_read_some method) can be called concurently when calling io_service::run twice.

like image 779
Guillaume Paris Avatar asked Sep 27 '22 11:09

Guillaume Paris


1 Answers

Your assumptions are correct. Each thread which calls io_service::run() will dequeue and execute handlers (simple function objects) in parallel. This of course only makes sense if you have more than one source of events feeding the io_service (such as two sockets, a socket and a timer, several simultaneous post() calls and so on).

Each call to a socket's async_read() will result in exactly one handler being queued in the io_service. Only one of your threads will dequeue it and execute it.

Be careful not to call async_read() more than once at a time per socket.

like image 170
Richard Hodges Avatar answered Sep 30 '22 09:09

Richard Hodges