|
Boost : |
From: Christopher Kohlhoff (chris_at_[hidden])
Date: 2007-04-24 09:39:35
On Tue, 24 Apr 2007 17:59:29 +0530, Gaurav.Jain_at_[hidden] said:
> Hi,
>
> I am designing a server which will going to run on multiprocessor and
> which will going to provide service to thousand of clients concurrently.
> I am using boost asio library to do this. I want to know how can I use
> one io_service object per CPU approach for this?
I'm sure I've outlined this in another email somewhere, but I can't find
it right now. Anyway, the basic idea is as follows:
- Create an io_service for each CPU.
- Create an io_service::work object for each io_service to keep it
running when it would otherwise have nothing else to do.
- Spawn a thread for each io_service to call io_service::run().
- Create a tcp::acceptor on one of the io_services.
- Create your tcp::socket objects on any of the io_services, using some
sort of load balancing scheme to choose the io_service to use (e.g.
round robin).
- Ensure that all communication between io_services uses message
passing, i.e. use io_service::post().
The core of the work would probably happen in a handle_accept
function(), e.g. using the Daytime.3 tutorial program as a starting
point:
void start_accept()
{
// ------> Load balance here <------
tcp_connection::pointer new_connection =
tcp_connection::create(choose_io_service());
acceptor_.async_accept(new_connection->socket(),
boost::bind(&tcp_server::handle_accept, this, new_connection,
asio::placeholders::error));
}
void handle_accept(tcp_connection::pointer new_connection,
const asio::error_code& error)
{
if (!error)
{
// ------> Use message passing here <------
new_connection->socket().io_service().post(
boost::bind(&tcp_connection::start, new_connection));
start_accept();
}
}
Cheers,
Chris
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk