2

Currently looking at a more detailed tutorial (https://www.gamedev.net/blogs/entry/2249317-a-guide-to-getting-started-with-boostasio/) than the ones provided on the Boost.Asio website but there are still some things I don't understand.

Given this function ...

#include <boost/asio.hpp>
#include <iostream>

int main(int argc, char* argv[]) {

  boost::asio::io_service io_service;
  boost::asio::io_service::work work(io_service);

  io_service.run();

  std::cout << "Do you reckon this line displays?" << std::endl;

  return 0;
}

... and according to the documentation of the run method (http://www.boost.org/doc/libs/1_65_1/doc/html/boost_asio/reference/io_service/run.html), it should run the io_service object's event processing loop. But what does that mean? I don't see any event processing loop. I can just observe the fact that the std::cout instruction is never reached.

This time, if we replace io_service.run() by io_service.poll(), the std::cout statement will be reached. According to the documentation of the poll method (http://www.boost.org/doc/libs/1_65_1/doc/html/boost_asio/reference/io_service/poll.html), it should run the io_service object's event processing loop to execute ready handlers.

But to be honest, I think the documentation is a bit confusing and I don't see which ready handlers they are referring to and in a broader way, what's the difference between poll() and run()

kenba
  • 4,303
  • 1
  • 23
  • 40
Pierre P.
  • 1,055
  • 2
  • 14
  • 26
  • 1
    @Tas huh that's almost completely unrelated. Much like "What is a tornado?" is not a duplicate of "How do I fix my tent to the ground?" – sehe Nov 05 '17 at 23:33
  • See also https://stackoverflow.com/questions/37392856/what-is-the-difference-between-poll-and-run?noredirect=1&lq=1, https://stackoverflow.com/questions/8727568/whats-the-difference-between-boostio-service-poll-one-and-run-one or indeed the documentation here http://think-async.com/ – sehe Nov 05 '17 at 23:37
  • run() will block until there is no work to be done. Since you explicitly create work with io_service::work, there will always be work to do. Therefore run() never exits. run() will also block until there are no pending completion handlers. poll(), on the other hand, will only block until there are no completion handlers to process at this very moment, and will then return. – Paul Belanger Nov 06 '17 at 02:41
  • Thanks @PaulB, but what do you refer to when talking about *completion handlers* ? – Pierre P. Nov 06 '17 at 21:01
  • A completion handler is the callback that you add in an async_whatever call. A call to io_service.post(handler) is also a completion handler to a trivial event. – Paul Belanger Nov 08 '17 at 00:57

0 Answers0