I want to develop a server use multiple threads and multiple io_service instance(each thread use an io_service instance) to listen at a port. For each io_service instance I create a corresponding boost::asio::ip::tcp::acceptor object. I try this code:
using namespace boost::asio;
...
io_service io_service_;
io_service io_service_1;
ip::tcp::acceptor* acceptor_;
acceptor_ = new ip::tcp::acceptor(io_service_);
ip::tcp::endpoint ep( ip::tcp::v4(), LISTEN_PORT);
acceptor_->open(ep.protocol());
acceptor_->bind(ep);
acceptor_->listen();
ip::tcp::acceptor* acceptor_1;
acceptor_1 = new ip::tcp::acceptor(io_service_1);
acceptor_1->open(ep.protocol());
acceptor_1->bind(ep);
acceptor_1->listen();
...
boost::thread th( boost::bind(&io_service::run, &io_service_));
boost::thread th( boost::bind(&io_service::run, &io_service_1));
...
when running will display error dialog:
boost::exception_detail::clone_impl< boost::exception_detail::error_info_injector > at memory location 0x001FF704.
can you help me how to make a server with Multiple threads, each thread use an io_service instance?
Update: as I read in Boost.Asio C++ Network Programming, have 3 way to use io_service with thread:
- Single-thread with one io_service and one handler thread(thread running io_service::run())
- Multi-threaded with a single io_service instance and several handler threads
- Multi-threaded with several io_service instances and several threads
I can implement case 1 and 2. But with case 3, I don't know how to implement it to handle many concurrent connections, should I use 1 thread to handle 1 io_service(as above)? Is case 3 has better performance(can handle more concurrent connections) than case 2?