|
Boost Users : |
Subject: [Boost-users] [ASIO] io_service.run() call is not blocking until async_write completes
From: nick aschberger (nick.aschberger_at_[hidden])
Date: 2009-02-22 04:42:48
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
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net