1
votes

I am compiling the boost::asio example: http://www.boost.org/doc/libs/1_46_1/doc/html/boost_asio/examples.html#boost_asio.examples.http_server_3

It is supposed to be a multi-threaded server implementation.

Now, in the async_read handler I print a message and sleep for 30 seconds. I open localhost in two browsers and see that handle_read is called once, then 30 seconds nothing happens and finally handle_read is called for the second time.

The io_Service.run is called with 5 threads.

Why aren't the handlers called concurrently? e.g. why does it wait for the first handle_read to finish before calling the second?

1
Show us your code. My bet is that you don't register a new listener until after the sleep. - Kerrek SB
Are you initiate new async_read() from handle BEFORE sleep? - Olympian
Yes, I am putting the sleep before the new async_read. boost.org/doc/libs/1_46_1/doc/html/boost_asio/example/http/… Basically immediately after the if( ! e ). The way I thought it works is that this works per-connection. So I want to process the data of client 1, and only receive new data when it has finished processing. But while it is processing client 1 I want to process client 2 as well. - thelamb
And you make it just in one socket? If you have 2 different clients you need 2 different sockets. And for each one you need to call async_read() - so they will be independent. - Olympian
In handle_accept: new_connection_->start(); is called for every new connection, which calls async_read, then async_accept is called to wait for another connection. In handle_read (registered by async_read), the request is processed and after that async_read is called again to register handle_read. So the calls are made independently in two sockets. - thelamb

1 Answers

1
votes

It helps to post code. When I use this modification to the asio example:

void connection::handle_read(const boost::system::error_code& e,
    std::size_t bytes_transferred)
{
  std::cerr << "connection::handle_read()\n";
  boost::this_thread::sleep(boost::posix_time::seconds(10));
  std::cerr << "connection::handle_read() done sleep\n";
  if (!e)
  {

it works as expected, that is

$ ./test 0.0.0.0 7777 5 .
connection::handle_read()
connection::handle_read()
connection::handle_read() done sleep
connection::handle_read() done sleep

How do you "sleep for 30 seconds"? Perhaps the sleep function you've used pauses all threads in the process?