1

My question is about boost asio's io_service. When i call it with that method :

int main()
{
  try
  {
    boost::asio::io_service io_service;
    Server server(io_service);
    std::vector<boost::shared_ptr<boost::thread> > threads;
    for (std::size_t i = 0; i < 16; ++i)
    {
        boost::shared_ptr<boost::thread> thread(new boost::thread(boost::bind(&boost::asio::io_service::run, &io_service)));
        threads.push_back(thread);
    }
    for (std::size_t i = 0; i < threads.size(); ++i)
    threads[i]->join();
  }
  catch (std::exception& e)
  {
    std::cerr << e.what() << std::endl;
  }

  return 0;
}

Will it share threads for requests dynamically, or it will be only give only one thread for connection groups ? Thanks.

Sam Miller
  • 23,808
  • 4
  • 67
  • 87
  • Can you clarify what are "requests" and "connection groups" in current context? – Ivan Nov 14 '13 at 12:22
  • @IvanGrynko i mean as request handler processing server logic on received packet. Connection groups mean like with reactor pattern 1 thread per dispatching connection signals. – user2971678 Nov 14 '13 at 12:25

1 Answers1

2

asio::io_service has a shared event queue. These events are processed by threads that are currently calling io_service::run(). So yes, it's possible that a handler will be called from different threads.

I don't recommend you to run 16 threads for you server as it gives you a slowdown ( because of context switching and the boost::asio bottleneck ). If you really need so many threads then prefer using "io_service per thread" idiom.

Community
  • 1
  • 1
Ivan
  • 2,007
  • 11
  • 15
  • I just want to do thread per core design, but i added for debug testing on old computer server 16 threads to allow handle players. im not sure that io service per thread will be good because someone joining world operates at big sync packets and will block io_service at all. – user2971678 Nov 14 '13 at 15:50