What is the definition of the boost :: asio :: io_service handler?

I am trying to understand the difference between io_service poll () / poll_one () and run () / run_one (). The difference, as stated in the documentation, is that poll () executes the ready-made handlers, not run (), which any handler executes.

But nowhere in the boost documentation could I find the definition of a “ready-made handler”.

The real answer to this question is one that can show, preferably with a code example, the difference between a ready and a non-ready handler and the difference between how poll () and run () do it.

Thanks.

+4
source share
2 answers
int main() { boost::asio::io_service io_service; boost::asio::deadline_timer timer(io_service); timer.expires_from_now(boost::posix_time::seconds(5)); timer.async_wait([](const boost::system::error_code& err) { std::cout << (err ? "error" : "okay") ;}); //io_service.poll_one(); io_service.run_one(); } 

If you use io_service.poll_one(); , you most likely will not see any output, because the timer has not passed yet. ready handler simply means a handle that is ready to start (for example, after a timer expires or the operation completes, etc.). However, if you use io_service.run_one(); , this call will be blocked until the timer finishes and the handler executes.

+4
source

A "ready handler" is a handler that is ready to run. If you issued an asynchronous call, it starts in the background, and its handler becomes ready when the asynchronous call is made. Prior to this, the handler is on hold, but not ready.

  • poll_one executes one ready-made handler, if any.
  • poll executes all ready handlers, but does not wait. Both versions of the survey are returned immediately after the execution of the handlers.
  • run_one executes the ready handler, if there is one, if it does not expect the first waiting handler to become ready, that is, it blocks.
  • run executes and waits until there are no ready or waiting handlers. After his return, io_servie is in a stop state.

See also Boost :: Asio: io_service.run () vs poll () or how to integrate boost :: asio in mainloop

+8
source

All Articles