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

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 the following conclusion:

**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 starts handlers that are ready to be run without blocking until io_service is stopped or there are no ready-made handlers.

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

and the second question: the documentation says that:

return The number of handlers that were executed.

if it does not block, what value will it return? - the number of objects in the queue? - but this is not the same as being "fulfilled."

+3
source share
2 answers

This is an old question, but you really haven't received an answer about running vs poll.

io_service:: run , - , , - .. .

io_service:: poll , , .

, , , -, .

, .

+13

- , . pool , , .

. - , , . , , . Read call . , .

. . , .

+4

Source: https://habr.com/ru/post/1748895/


All Articles