[Asio] run_one does not seem to run signal handler but run does
Hi,
I am reasonably new to Boost.Asio. Below is a short test program to
catch CNTR C. It nearly works as i would have expected.
#include <iostream>
#include
On 1/03/2015 15:39, Robert Valkenburg wrote:
I don't understand why io_service.run() says 6 handlers where invoked but otherwise it seems OK.
Now if i replace io_service.run() with io_service.run_one() i expected the handler to be invoked once when CNTR C is pressed. Here is the output [...] This suggests one handler was invoked which is what i might have expected. This does not have a line with >1 indicating entering the handler and there is no output from the handler and count remains at 0.
"One handler" does not necessarily mean "one application code handler". Many high-level async operations are actually composed internally of a sequence of lower-level operations. As such, it may require multiple internal handler calls before your user-level handler is actually called. From your output, it appears that it your case it actually requires two handler calls to actually invoke your application handler. However you should not rely on this as it may change if the internals of Asio change or if some other condition in your execution environment changes. run_one() is intended to be called in a loop with other "idle" work -- but using run() is preferred whenever you can just post the extra work to the io_service anyway. Note that an async_wait is a single-shot thing anyway -- if you didn't call async_wait again in your handler then it would only catch the signal once, if that's what you're trying to do.
participants (2)
-
Gavin Lambert
-
Robert Valkenburg