简体   繁体   中英

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 ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM