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

The following diagram is provided by boost asio documentation:

enter image description here

I understand that if I call the io_service::run method io_service::run (in two separate threads), I will have two threads for detecting events from the final event queue through the asynchronous event demultiplexer. I'm right?

More precisely, I doubt the achievement of parallelization by calling the io_service::run method io_service::run . For example, if you are dealing with a socket, if, for example, I have two sockets associated with the same io_service object , each socket method that calls socket.async_read_some method, does it include 2 registered callbacks (async_read_some method), can be convincingly called when io_service::run called twice.

+5
source share
1 answer

Your assumptions are correct. Each thread that calls io_service::run() deactivates and executes handlers (objects of a simple function) in parallel. This, of course, only makes sense if you have several sources of events that feed io_service (for example, two sockets, a socket and a timer, several simultaneous calls to post() , etc.).

Each async_read() socket call will result in exactly one handler being placed in io_service. Only one of your threads will delete it and execute it.

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

+4
source

All Articles