Is I am looking at writing a multithreaded tcp server using boost ASIO. I have read through the tutorials and had a look at some of the examples and just want to check that my understanding is correct.
The server will accept connections then service requests from multiple clients.
My understanding is as follows:
- The server uses "a single
io_service
and a thread pool callingio_service::run()
" - All threads call
io_service::run()
. - The calls to
io_service::run()
are not within a strand, ergo completion handlers can run simultaneously. - When a request arrives one of the threads is chosen, its read handler will be called
- Another request may arrive,starting the read handler on a second thread
- When one of the threads has finished handling the request it calls
async_write
, from within astrand
- Another thread also finishes processing its request, it also calls
async_write
, from within a strand - The writes to the
io_service
are serialised via thestrand
, ergo they are thread safe. - When the write operation completes the thread calls
async_read()
- This call is not protected by a
strand
and the thread will be used for handling requests
Is my understanding correct? Is this solution vulnerable to race conditions?