Boost logo

Boost Users :

Subject: [Boost-users] [asio] async_read and 100% CPU
From: Etienne Philip Pretorius (icewolfhunter_at_[hidden])
Date: 2009-06-30 14:07:19


Hello list,

Could someone please shed some light on this subject. It seems that me
read handler keeps on being called even when there has been no data
transferred...

Here is the accept handler that starts off the read handler (All methods
are part of the same class, namely 'client'):

         /* Accept Handler */
         void handle_accept(
             const boost::system::error_code& error
         ) {

             client* c;
             switch(error.value()) {
                 case boost::system::posix_error::success: {
                     c = new client();

                     socket.async_receive(
                         boost::asio::buffer(
                             buffer
                         ),boost::bind(
                             &client::handle_read,
                             this,
                             boost::asio::placeholders::error,
                             boost::asio::placeholders::bytes_transferred
                         )
                     );
                     break;
                 }
                 case boost::system::posix_error::interrupted:
                 case boost::system::posix_error::connection_aborted: {
                     c = this;
                     break;
                 }
                 default: {
                     throw error;
                 }
             }
             acceptor.async_accept(
                 c->socket,
                 boost::bind(
                     &client::handle_accept,
                     c,
                     boost::asio::placeholders::error
                 )
             );

         };

And here is the read handler....

         /* Read Handler */
         void handle_read(
             const boost::system::error_code& error,
             std::size_t bytes_transferred
         ) {

             switch(error.value()) {
                 case boost::system::posix_error::success: {
                     if(bytes_transferred > 0) {
                         std::copy(
                             buffer.begin(),
                             buffer.end(),
                             std::back_insert_iterator<std::string>(data)
                         );
                         break;
                     }
                 }
                 case boost::system::posix_error::interrupted: {
                     socket.async_receive(
                         boost::asio::buffer(
                             buffer
                         ),boost::bind(
                             &client::handle_read,
                             this,
                             boost::asio::placeholders::error,
                             boost::asio::placeholders::bytes_transferred
                         )
                     );
                     return;
                 }
                 default: {
                     throw error;
                 }
             }
             //TODO: parse contents..
         };

Using epoll interface, as I am running Ubuntu Jaunty.

Calling io_service.run() from the main thread.

Thank you,
Etienne Pretorius.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net