boost::asio 在线程中启动不同的服务?

发布于 2024-10-24 04:59:07 字数 310 浏览 7 评论 0原文

似乎所有示例总是显示在所有线程中运行相同的 io_service 。

可以启动多个io_services吗?这是我想做的:

在主线程中启动 io_service A 来处理用户输入...

在另一个线程中启动另一个 io_service B,然后可以启动一堆工作线程 所有线程都共享 io_service B。io_service

A 上的用户可以“发布”io_service B 上的工作,以便它在工作池上完成,但不需要在 io_service A(即主线程)上完成任何工作。

这可能吗?这有道理吗?

谢谢

Seems like all the examples always show running the same io_service in all threads.

Can you start multiple io_services? Here is what I would like to do:

Start io_service A in the main thread for handling user input...

Start another io_service B in another thread that then can start a bunch of worker
threads all sharing io_service B.

Users on io_service A can "post" work on io_service B so that it gets done on the worker pool but no work is to be done on io_service A, i.e. the main thread.

Is this possible? Does this make sense?

Thanks

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

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

发布评论

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

评论(2

雅心素梦 2024-10-31 04:59:07

根据我的经验,如果每个 cpu 一个 io_service 或每个进程一个 io_service 性能更好,这实际上取决于应用程序。在 asio-users 邮件列表上进行了讨论几年前关于这个话题。

Boost.Asio 文档有一些很好的示例展示了这两个< HTTP Server 2 和 HTTP Server 3 示例中的 /a> 技术。但请记住,第二个 HTTP 服务器只是展示如何使用此技术,而不是何时或为何使用它。这些问题需要通过分析您的应用程序来回答。

一般来说,在使用 Boost.Asio 创建应用程序时应使用以下顺序

  1. 带有单个 io_service的单线程
  2. 线程池
  3. 具有某种 CPU 关联性的多个 io_service 对象

In my experience, it really depends on the application if an io_service per cpu or one per process is better performing. There was a discussion on the asio-users mailing list a few years ago on this very topic.

The Boost.Asio documentation has some great examples showing these two techniques in the HTTP Server 2 and HTTP Server 3 examples. But keep in mind the second HTTP server just shows how to use this technique, not when or why to use it. Those questions will need to be answered by profiling your application.

In general, you should use the following order when creating applications using Boost.Asio

  1. Single threaded
  2. Thread pool with a single io_service
  3. Multiple io_service objects with some sort of CPU affinity
标点 2024-10-31 04:59:07

好问题!

是的,这是可能的。在我当前正在开发的应用程序中,我已将应用程序分解为负责系统不同方面的单独组件。每个组件都在自己的线程中运行,有自己的一组计时器,使用 asio 执行自己的网络 I/O。从可测试性/设计的角度来看,它对我来说似乎更干净,因为没有一个组件可以干扰另一个组件,但我愿意接受纠正。我想我可以重写 io 服务中作为参数传递的所有内容,但目前还没有发现需要这样做。

所以回到你的问题,你可以做任何你想做的事情,在我看来,这更多的是尝试一下,如果遇到任何问题就改变它。

另外,您可能想看看 Sam Miller 在处理用户输入的另一篇 WRT 文章中指出的内容(即如果您使用的是控制台): https://stackoverflow.com/questions/5210796/boost-asio-how-to-write-console-server

Good question!

Yes, it is possible for one. In an application I'm currently working on I have broken up the application into separate components responsible for different aspects of the system. Each component runs in its own thread, has its own set of timers, does its own network I/O using asio. From a testability/design perspective, it seems more clean to me, since no component can interfere with another, but I stand to be corrected. I suppose I could rewrite everything passing in the io service as a parameter, but currently haven't found the need to do so.

So coming back to your question, you can do whatever you want, IMO it's more a case of try it out and change it if you run into any issues.

Also, you might want to take a look at what Sam Miller pointed out in a different post WRT handling user input ( that is if you're using a console): https://stackoverflow.com/questions/5210796/boost-asio-how-to-write-console-server

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