tags:

views:

248

answers:

1

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".

+2  A: 

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.

Alex Farber
the fact of the matter is that poll() doesn't return the control until all handlers are completed.
niXman
Non-blocking means: it doesn't hands for a long time, and doesn't contain endless waiting. Blocking operation would execute pending handlers and blocked by waiting for a next handler. Non-blocking operation returns when the queue is empty.Asynchronous operation starts some handling (possibly in another thread) and returns immediately. Synchronous operation returns only when all the work finished.Your understand non-blocking operation as asynchronous. poll function is non-blocking and synchronous.
Alex Farber
I'm confused ...
niXman
ok.Please explain the difference between the run () and poll ()?In my example, I do not see the difference.
niXman
in the following code:void func1(int c) { for ( int i = 0; i < c; ++i ) { std::cout << "ping " << i << std::endl; sleep(1); }}int main(int argv, const char** argc) { boost::asio::io_service io; io.post(boost::bind(func1, 4)); std::cout << "before run" << std::endl; io.run();// io.poll(); std::cout << "after run" << std::endl; std::cin.get(); return 0;}where is no defference between poll() and run().
niXman