Answer the question
In order to leave comments, you need to log in
Why do we need to wait for an asynchronous operation in this example?
There is the following example from the boost documentation, tell me why this line is used in the receive method?
do io_service_.run_one(); while (ec == boost::asio::error::would_block);
Answer the question
In order to leave comments, you need to log in
In the same example, everything is written, a little higher. I quote:
// Set up the variables that receive the result of the asynchronous
// operation. The error code is set to would_block to signal that the
// operation is incomplete. Asio guarantees that its asynchronous
// operations will never fail with would_block, so any other value in
// ec indicates completion.
ec = boost::asio::error::would_block;
std::size_t length = 0;
ec
is set to would_block
and is used as a marker that we have NOT written anything to the ec variable in the handler YET handle_receive
. In other words, as a marker of what handle_receive
has not yet been completed. When it is executed, then, according to the Asio interface, something other than would_block
, which will be written to ec
. This will be the condition for exiting the loop. deadline_
. From which it can be concluded that each callrun_once
can run either the handler handle_receive
, or check_deadline
(remember that it run_once
pumps only one call to any handler in the queue).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question