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

£可爱£侵袭症+ 提交于 2019-12-22 04:33:14

问题


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 ?


回答1:


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



来源:https://stackoverflow.com/questions/11010530/do-we-need-multiple-io-service-per-thread-for-threaded-boostasio-server-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!