[ASIO] io_service.run() call is not blocking until async_write completes
Hi Folks, I am using boost 1.36, and I have a C++ class that wraps calls to boost::asio sockets. There are two important functions: WriteData -- takes a vector of uint8_t's and uses async_write. WaitForPreviousWritestoComplete -- Here the user expects to block until all their previous writes are complete. Effectively this just calls io_service.run(). A user of this class can will WriteData a few times, and then do some other processing for a bit while the async_write is happening in the background, and then they should block at io_service.run() until the writes complete (and error status etc is checked). It was my understanding that io_service.run() call would block the thread of execution at that point until the pending writes on my output socket were complete. Am I wrong in my interpretation, because I see the user effectively doing: async_write async_write async_write io_service.run() async_write async_write io_service.run() The first call to io_service.run() correctly calls my message handler 3 times, for the 3 writes. The second call to io_service.run() I expect to see the handler called twice, but it is not called at all! If I get a bit tricky and do something like: async_write async_write async_write while (!all_writes_comple_flag){ io_service.run() } (the handler will set all_writes_comple_flag when all writes are complete :) ) then the code is effectively busy waiting at that point until it is done, but I do see the handler entered when I expect. Is there some timing issue occurring here? Perhaps the thread that is managing the socket stuff not had a chance to register that it has a pending write waiting? Am I misinterpreting what boost means in the documentation by saying the io_service.run() function blocks? If I am, what should I do to wait until all asynchronous writes are complete? thanks Nick
Am I wrong in my interpretation, because I see the user effectively doing: async_write async_write async_write io_service.run() async_write async_write io_service.run()
The first call to io_service.run() correctly calls my message handler 3 times, for the 3 writes. The second call to io_service.run() I expect to see the handler called twice, but it is not called at all!
io_service.reset() should be called before the second run(): http://www.boost.org/doc/libs/1_38_0/doc/html/boost_asio/reference/io_servic...
This doesn't appear to be clear from the tutorials. Is there a better resource for "howto" than the boost site tutorials?
You've got: a) tutorial, b) fully functional examples, c) reference. Probably your use-case is not covered either in tutorial or in the examples, but in the reference you can find all the needed information. By the way, note that in asio you've got blocking i/o as well, so if you just try to emulate blocking behaviour using async. calls, it's already there.
participants (3)
-
Igor R
-
Nick Aschberger
-
nick aschberger