问题
CancelIo() is supposed to cancel all pending I/O operations associated with the calling thread. In my experience, CancelIo() sometimes cancels future I/O operations as well. Given:
ReadFile(port, buffer, length, &bytesTransferred, overlapped);
- If I invoke
CancelIo(port)
immediately before the read,GetQueuedCompletionStatus()
will block forever, never receiving the read operation. - If I invoke
CancelIo(port)
immediately after the read,GetQueuedCompletionStatus()
will return 0 withGetLastError()==ERROR_OPERATION_ABORTED
- If I invoke
CancelIo(port)
and there are no pending or subsequent reads,GetQueuedCompletionStatus()
will block forever.
The key point here is that there is no way to detect when CancelIo()
has finished executing. How can I ensure that CancelIo()
is done executing and it is safe to issue further read requests?
PS: Looking at http://osdir.com/ml/lib.boost.asio.user/2008-02/msg00074.html and http://www.boost.org/doc/libs/1_44_0/doc/html/boost_asio/using.html it sounds like CancelIo() is not really usable. Must customer requires Windows XP support. What are my options?
NOTE: I am reading from a serial port.
回答1:
CancelIo()
works fine. I misunderstood my code.
Upon further investigation it turns out that the code was invoking CancelIo()
followed by ReadFile()
with a timeout INFINITE
. The completion port was never getting notified of the read because the remote end was never sending anything. In other words, CancelIo()
did not cancel subsequent operations.
I found some eye-opening documentation here:
Be careful when coding for asynchronous I/O because the system reserves the right to make an operation synchronous if it needs to. Therefore, it is best if you write the program to correctly handle an I/O operation that may be completed either synchronously or asynchronously. The sample code demonstrates this consideration.
It turns out that device drivers may choose to treat an asynchronous operation in a synchronous manner if the data being read is already cached by the device driver. Upon further investigation, I discovered that when CancelIo()
was being invoked before ReadFile()
it would sometimes cause the latter to return synchronously. I have no idea why the completion port was never getting notified of ReadFile()
after a CancelIo()
but I can no longer reproduce this problem.
The completion port is signaled regardless of whether ReadFile()
is synchronous or asynchronous.
回答2:
Wait on (possibly with zero timeout) overlapped.Handle
. It will be set whether the operation is completed or cancelled.
回答3:
If you're already using overlapped operations, why do you need to cancel I/O at all? The entire concept of 'cancelling' an in-flight I/O operation is really race-prone, and totally subject to the underlying device stack you're trying to write to; really the only time you'd want to do this is to unblock another thread who is waiting on the completion of that I/O.
回答4:
It is possible to write asynchronous I/O code without CancelIo function. The question depends on the scenario you are using CancelIO. Let's say that you need to implement file reading thread. Thread pseudo-code:
for(;;) { ReadFile(port, buffer, length, &bytesTransferred, overlapped); WaitForMultipleObjects( overlapped event + stop event); if ( stop event is signaled ) break; if (overlapped event is signaled ) handle ReadFile results }
Such thread reads file (socket, port etc.) using overlapped I/O. Most of the time it waits on WiatForMultipleObjects line. It wakes up when new data is available, or stop event is signaled. To stop this thread, set stop event from another thread. CancelIO is not used.
来源:https://stackoverflow.com/questions/3921111/how-to-find-out-when-cancelio-is-done