开发者

boost::asio starting different services in threads?

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? Do开发者_开发问答es this make sense?

Thanks


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜