9

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 ?

Dipro Sen
  • 4,350
  • 13
  • 37
  • 50

1 Answers1

10

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...

Igor R.
  • 14,716
  • 2
  • 49
  • 83
  • Then what will the per session threads do ? e.g. shouldn't I have one session in one thread ? to make each session parallel ? However I am supposed to have < 5 concurrent Sessions at once. Though I'd like to know the general purpose solution – Dipro Sen Jun 13 '12 at 08:51
  • No, you don't need that. Please read the following: http://www.boost.org/doc/libs/1_47_0/doc/html/boost_asio/overview/core/async.html . The only case where you might need additional threads is if TCPSession completion handlers are too heavy (eg. perform time-consuming DB operations or so). But then your application wouldn't be scalable anyway, so you'd have to change the design to split or move some work out of completion handlers. – Igor R. Jun 13 '12 at 10:01
  • So If I just have a different socket per session It will be okay ? and can I develop the whole thing in single thread first ? and then switch to multiple threads ? – Dipro Sen Jun 13 '12 at 10:15
  • 1
    Of course, TCPSession would encapsulate its own tcp::socket, but all this sockets may be served by the same io_service (i.e. you can create them all with the same io_service). If you use io_sevice-per-cpu model, then you can develop TCPSessions with no regards to how many io_service's you will have eventually. Just create io-service pool (each io_service would be in its own thread), and every time you create TCPSession, take an io_service from the pool. Initially you can set the pool size to 1, and later increase it to {boost::thread::hardware_concurrency()} . – Igor R. Jun 13 '12 at 10:59