5

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

Oliver K
  • 295
  • 2
  • 15

2 Answers2

2

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
Sam Miller
  • 23,808
  • 4
  • 67
  • 87
  • +1 interesting discussion :), makes me wonder about my system design though I haven't experienced any issues as yet. – Ralf Mar 19 '11 at 16:04
  • Here is the situation, I have an application that has one thread and one io_service that handles sockets and stdio, it is old non-thread save code. Now I would like to add a worker thread pool that has its own io_service and will not interact with the non-thread safe code on the main thread. The worker pool will run some separate code on separate data and the communication will be through io_service posts between the main io_service and the io_service run in the thread pool. Sounds like this can be done? – Oliver K Mar 19 '11 at 22:02
  • @Oliver yes, that sounds like it can be done. Though, if it were my task, I would fix the non-thread safe code with strands and use a single io_service with a pool of threads invoking io_service::run(). – Sam Miller Mar 20 '11 at 13:23
1

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

Community
  • 1
  • 1
Ralf
  • 9,405
  • 2
  • 28
  • 46