I am using Boost::ASIO version 1.52.0 for a Windows client. I would like to be able to dedicate a thread to handling all receiving messages from the server, and then another dedicated thread to handle all outgoing messages to the server. I am using the same io_service object for both threads right now. What I’m worried about is that when the io_service::run() method is called, the thread that is handling the outgoing messages, might get scheduled to handle some incoming message calls and vice-a-versa. So, my question – is this possible? If it is, then would using a second io_service object solve the problem – one for each thread? Is there a better way to design this? I am trying to avoid using multiple threads for both the read and write handlers.
The other thing I would like confirmation on is – I have read that a lock should be used to single thread the code if 2 or more async_reads can be done at the same time. Likewise for async_writes. Should a lock also be used if an async_read can execute at the same time as an async_write, or should that be thread safe?
One last question – can the async methods – async_connect, async_read, and async_write all be called from a different thread before a worker thread has called the io_service run method?
You should use a single
io_service, however many threads you use to invokeio_service::run()can also invoke handlers for asynchronous operations owned by theio_service. If these handlers access shared data structures, you will need to use astrandto ensure exclusive access. You’ll also need to ensure at most one write operationand read operation
is outstanding for each socket.
Using an
io_servicefor allasync_write()operations and anotherio_servicefor allasync_read()operations is not possible because a single socket is serviced by oneio_servicethat is passed in as a parameter in the constructor.In my experience most mult-io_service designs are driven by performance and latency requirements. The HTTP Server 2 example explores this with an
io_serviceper CPU.