Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do we need multiple io_service per thread for threaded boost::asio server with a single acceptor

I am not much experienced in boost::asio. I've some pretty basic questions.

Do I need to have a different io_service, and a different socket under a different thread but one single acceptor, to process a client in a threaded server ?

I believe I must have a different socket for a new client. But if all threads use the same io_service would it be parallel ?

I was going through http://en.highscore.de/cpp/boost/index.html in asio section which says I need to have different io_services in different threads to achieve parallelization.

I if I plan to make a Server class that creates a new TCPsession each time a new client appears in acceptor.async_accept
and TCPSession ctor creates an io_service and a thread and runs that io_service.run() in its own thread would it be a good design ?

However in this design where would I join all these threads ? do I need another io_service for main so that it doesn't terminate even before getting a new Client ?

like image 346
Dipro Sen Avatar asked Jun 13 '12 08:06

Dipro Sen


1 Answers

Single io_service running in a single thread can servce all the asio objects in your project. In such a design the i/o still would be "parallel" in the sense that it's non-blocking, asynchronous; but since io_service::run() is being run in one single thread, all the completion handlers would be invoked serially, one-by-one.

To scale your networking module over multiple CPUs, you can use one of the two approaches: thread-per-core, io_service-per-core - see HTTPServer2 and HTTPServer3 examples.

In any case, creating a thread or io_service per TCPSession seems to me an unnecessary overhead - think of a case where you've got thousands of TCPSessions...

like image 52
Igor R. Avatar answered Nov 05 '22 11:11

Igor R.