Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

troubles with boost::asio::io_service::poll()

the following code:

/***************************************************************************/

boost::mutex m;

struct func {
   func(int v):n(v) {}
   void operator()() {
      {  boost::mutex::scoped_lock l(m);
         std::cout << "run function " << n << std::endl;
      }
      for ( int idx = 0; idx < 4; ++idx ) {
         {  boost::mutex::scoped_lock l(m);
            std::cout << "function " << n << ", ping " << idx << std::endl;
         }
         sleep(1);
      }
   }

private:
   int n;
};

/***************************************************************************/

int main(int argv, const char** argc) {
   boost::asio::io_service io;

   for ( int idx = 0; idx < 4; ++idx ) {
      io.post(func(idx));
   }

   std::cout << "before run" << std::endl;
   io.poll();
   std::cout << "after run" << std::endl;

   std::cin.get();

   return 0;
}

/***************************************************************************/

gives such an output:

**before run**
run function 0
function 0, ping 0
function 0, ping 1
function 0, ping 2
function 0, ping 3
run function 1
function 1, ping 0
function 1, ping 1
function 1, ping 2
function 1, ping 3
run function 2
function 2, ping 0
function 2, ping 1
function 2, ping 2
function 2, ping 3
run function 3
function 3, ping 0
function 3, ping 1
function 3, ping 2
function 3, ping 3
**after run**

but, according to the documentation:

The poll() function runs handlers that are ready to run, without blocking, until the io_service has been stopped or there are no more ready handlers.

poll() - is a non-blocking method. what's the problem?

and the second question: in documentation it is said that:

return The number of handlers that were executed.

if it is non-blocking, what value it will return? - the number of objects in the queue? - but this is not the same thing as "that were executed".

like image 750
niXman Avatar asked Dec 03 '22 12:12

niXman


2 Answers

This is an old question but you never really got an answer about run vs poll.

io_service::run will keep running as long as there is something to do, such as waiting on a deadline timer or IO completion notification, etc. This is why there is the work object to keep run from exiting.

io_service::poll will only execute ready handlers, and will not return until there are no more handlers ready to be dispatched.

The difference in other words is that run will wait for a pending handler to be ready, like a timer or IO completion notification, while poll will return in that situation.

This behavior is useful if you want to perform some idle processing.

like image 89
adzm Avatar answered Dec 28 '22 02:12

adzm


Non-blocking is not the same as asynchronous. pool synchronously runs handlers that are ready to run, and returns without blocking and waiting for another handlers.

Additional explanation. Blocking input operation is operation which starts endless wait if no input data found. Consider Read operation which is supposed to read one byte from some port, but there is no incoming data. In this case Read call hangs, and returns only when byte is received. Non-blocking read operation returns immediately, if there is no input information.

pool operation is non-blocking. It synchronously executes all pending requests and returns. If there are no incoming requests, poll returns immediately.

like image 37
Alex F Avatar answered Dec 28 '22 04:12

Alex F