多个 ASIO io_service 是一件好事吗?

发布于 2024-11-07 21:08:03 字数 392 浏览 4 评论 0原文

我已经开始使用 Boost.ASIO 进行一些简单的网络编程,我对这个库的理解不是很多,所以请耐心等待我和我的新手问题。

目前,在我的项目中,我只有 1 个 io_service 对象。用于所有异步 I/O 操作等。

我的理解是,可以创建多个线程并将 io_service 实例的 run 方法传递给线程,以便为 io_service 提供更多线程。

我的问题:拥有多个 io_service 对象是一个好的设计吗?例如,有 2 个不同的 io_service 实例,每个实例都有 2 个关联的线程,它们是否以某种方式相互了解(并因此相互合作),或者如果不了解,它们是否会对彼此产生负面影响?

我的目的是拥有 1 个 io_service 用于基于套接字的 I/O,另一个用于基于串行 (tty) I/O。

I've begun using Boost.ASIO for some simple network programming, my understanding of the library is not a great deal, so please bear with me and my newbie question.

At the moment in my project I only have 1 io_service object. Which use for all the async I/O operations etc.

My understanding is that one can create multiple threads and pass the run method of an io_service instance to the thread to provide more threads to the io_service.

My question: Is it good design to have multiple io_service objects? say for example have 2 distinct io_service instances, each with 2 threads associated, do they somehow know about each other (and hence cooperate with each), or if not would they negatively affect each other?

My intention is to have 1 io_service for socket based I/O and another for serial based (tty) I/O.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怪我太投入 2024-11-14 21:08:03

我们使用多个 io_service 是因为应用程序中的某些组件需要以特定的固定优先级运行其所有工作线程,每个组件的优先级不同。因此,每个组件都有自己的 io_service,并且每个组件都有自己的执行 run() 的线程池。

我能想到的其他设计是,每个 IO 是否需要池中不同数量的线程,或者,与您的情况更相关的是,如果池无法共享,因为例如,如果您的网络 IO 可以取出每个线程并让串行 IO 等待。

We use multiple io_service's because some of the components in our application need to run all their worker threads at certain fixed priorities, different for each component. Thus each component is given its own io_service, and each component has its own pool of threads executing run().

Other designs I could think of would be if a different number of threads in the pool is required for each IO, or, more relevant to your case, is if the pool cannot be shared because, for example, if your network IO can take out every thread and leave your serial IO waiting.

蓝眼睛不忧郁 2024-11-14 21:08:03

IIRC,在 Michael Caisse 的 Boostcon ASIO 演讲期间(无论如何值得观看),我相信这个问题是由一位观众明确提出的,并被认为是一个潜在的解决方案。我从中得出,它本身并没有错,并且可以根据您的设计以这种方式使用。

IIRC, during Michael Caisse's Boostcon ASIO talk (which is worth watching anyway), I believe this question is explicitly asked by an audience member and ok'd as a potential solution. I take from that that it's not wrong per se, and can be used that way according to your design.

贩梦商人 2024-11-14 21:08:03

此讨论可能具有启发性:

http://thread.gmane.org /gmane.comp.lib.boost.asio.user/1300

<块引用>

我这里没有代码,但是为什么要使用多个 io_services?
我认为它使用一个 io_service 和多个线程执行运行
那一个 io_service。

IIUC,每个io_service都拥有一个select/epoll/whatever队列,所以有多个
io_services 类似于具有多个独立的 select/epoll 循环。在一些
情况,例如。大量套接字和多个 CPU,这可能会有所帮助。

我不太确定的是多个线程都在运行
io_service::run (使用相同的 io_service)。我认为这仅仅意味着
处理程序同时运行,而 select/epoll 等。循环是“共享的”。
我认为这最适合当您的处理程序运行时间相对较长时
运营。

This discussion may be enlightening:

http://thread.gmane.org/gmane.comp.lib.boost.asio.user/1300

I don't have the code right here, but why would you use multiple io_services?
I thought it used one io_service and multiple threads executing run on
that one io_service.

IIUC, each io_service owns a select/epoll/whatever queue, so having multiple
io_services is akin to having multiple independent select/epoll loops. In some
situations, eg. large numbers of sockets and multiple CPUs, this might help.

Something I'm less sure about is with multiple threads all running
io_service::run (with the same io_service). I think this just means the
handlers are run concurrently, while the select/epoll/etc. loop is 'shared'.
I think this is best for when your handlers are relatively long-running
operations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文