我们需要每个线程多io_service对象的线程的boost ::与单个受体ASIO服务器 [英] Do we need multiple io_service per thread for threaded boost::asio server with a single acceptor

查看:1423
本文介绍了我们需要每个线程多io_service对象的线程的boost ::与单个受体ASIO服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是在经历了很多的boost :: ASIO 。我有一些pretty基本问题。

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

我是否需要有不同的 io_service对象,和不同的插座根据不同的线程,但一个单一的接受,来处理在一个线程服务器的客户端?

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 ?

我相信我必须有一个新的客户端不同的插座。但是,如果所有线程使用相同的 io_service对象会是平行的?

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 ?

我要通过 http://en.highscore.de/cpp/boost/index.html在ASIO节
它说我需要在不同的线程不同io_services实现并行化。

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.

我,如果我打算做一个创建新TCPsession 新客户端将出现在 acceptor.async_accept

TCPSession 构造函数创建一个 io_service对象线程并运行该 io_service.run()在自己的线程这将是一个不错的设计?

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 ?

然而,在本设计中,我将参加所有这些线程?我需要另一个 io_service对象,以便它甚至没有得到一个新的客户端之前终止?

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 ?

推荐答案

io_service对象在一个线程中运行可以servce项目中所有的ASIO对象。在这样的设计在I / O仍然是在这个意义上的水货,它是非阻塞,异步的;但由于 :: io_service对象的run()正在在一个单一的线程中运行,所有的处理完成将连续调用,一个接一个。

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.

要扩展在多个CPU的网络模块,可以使用两种方法之一:线程每个内核,io_service对象每核心 - 看的 HTTPServer2和HTTPServer3范例

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.

在任何情况下,创建线程或 io_service对象 TCPSession 在我看来,一个不必要的开销 - 想想情况下你有成千上万 TCPSession 取值...

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

这篇关于我们需要每个线程多io_service对象的线程的boost ::与单个受体ASIO服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆