[asio] io_service::run() never returns and is blocking my thread

Hello, I'm currently using a serial_port object to call async_read_some(). This results in io_service::run() blocking until at least 1 byte or more can be read from the serial port. If nothing is ever sent, then I can not safely exit the thread in which io_service::run() is executing in. In other words, a call to join() on that thread hangs indefinitely until data is received. My completion callback is not executed at all until data arrives for reading. How can I force run() to return when I interrupt my thread? --------- Robert Dailey

Robert Dailey wrote:
In other words, a call to join() on that thread hangs indefinitely until data is received. My completion callback is not executed at all until data arrives for reading.
How can I force run() to return when I interrupt my thread?
Have you tried to close or cancel the serial port object? It should return your waiting handlers with an "operation interrupted" error code. Kind regards, Rutger

On Thu, Aug 27, 2009 at 3:07 PM, Rutger ter Borg
Robert Dailey wrote:
In other words, a call to join() on that thread hangs indefinitely until data is received. My completion callback is not executed at all until data arrives for reading.
How can I force run() to return when I interrupt my thread?
Have you tried to close or cancel the serial port object? It should return your waiting handlers with an "operation interrupted" error code.
Good idea, this would have worked. However, I chose to call io_service::poll() instead to keep my thread from being blocked in the first place.

Robert Dailey
I'm currently using a serial_port object to call async_read_some(). This results in io_service::run() blocking until at least 1 byte or more can be read from the serial port. If nothing is ever sent, then I can not safely exit the thread in which io_service::run() is executing in.
[...] I haven't used the serial_port object, but for a network socket, you can just close the network socket from another thread, and your completion handler will run with an appropriate error. Hope this helps, -----Scott.

I see. I was waiting for the thread to end before I closed the serial
port, I just assumed it would be unsafe to do otherwise. Thanks for
the info!
On Thursday, August 27, 2009, Scott Gifford
Robert Dailey
writes: I'm currently using a serial_port object to call async_read_some(). This results in io_service::run() blocking until at least 1 byte or more can be read from the serial port. If nothing is ever sent, then I can not safely exit the thread in which io_service::run() is executing in.
[...]
I haven't used the serial_port object, but for a network socket, you can just close the network socket from another thread, and your completion handler will run with an appropriate error.
Hope this helps,
-----Scott. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- --------- Robert Dailey
participants (3)
-
Robert Dailey
-
Rutger ter Borg
-
Scott Gifford