If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

confused about overlapped I/O and Events

I have been trying to understand how Microsoft does overlapped (asynchronous) I/O, especially involving the OVERLAPPED structure and Event objects. I have spent hours reading the MSDN articles on these topics, but they are just so confusing.

"Be careful when reusing OVERLAPPED structures. If an application reuses OVERLAPPED structures on multiple threads and calls GetOverlappedResult with the bWait parameter set to TRUE, the application must ensure that the associated event is set before the application reuses the structure. This can be accomplished by using the WaitForSingleObject function after calling GetOverlappedResult to force the thread to wait until the operation completes."

Maybe you could spell out a sequence of steps involving two threads in which the above problem arises. My brain is fried. Thanks :-)

Re: confused about overlapped I/O and Events

Hmm- what a difference a word makes! Assuming that the word is supposed to be "false", let me check with you and see if I've got the concept.

Problematic sequence of steps:

1. Thread A (actually, there need only be one thread to produce the problem) makes an asynchronous I/O request.
2. Some time later, thread A calls GetOverlappedResult to check and see if the I/O request is finished.
3. GetOverlappedResult indicates the request is not finished (by returning False, I think?).
4. Thread A makes a new I/O request using the same OVERLAPPED struct.
5. The I/O request from step 1 completes.
6. Thread A calls GetOverlappedResult to check if the request from step 4 completed successfully.
7. GetOverlappedResult reports that an I/O request did complete.
8. Thread A has no way of telling which request the results pertain to- are the results from the first I/O request or the second?

Corrected sequence of steps:

1. Thread A makes an asynchronous I/O request.
2. Some time later, thread A calls GetOverlapped Result to check and see if the I/O request finished.
3. GetOverlappedResult indicates that the request did not complete yet.
4. Thread A does some other work, and then calls WaitForSingleObject, and blocks until the first I/O request completes.
5. Then, thread A makes the second I/O request.
6. GetOverlappedResult indicates that the I/O request completed successfully.
7. This time, there is no ambiguity about which request the results pertain to.

Re: confused about overlapped I/O and Events

Yes. The OS "remembers" the event handle associated with the overlapped request. This handle is signaled by the OS to indicate that the request has completed. So using the same event handle in multiple outstanding requests would be bad.

Re: confused about overlapped I/O and Events

Awesome- thank you so much for helping me with that. I spent so much time trying to figure out how to reconcile the different articles with each other- it didn't occur to me that Microsoft might have made a typo. I will send them some feedback and see if they will change that wording for the benefit of future readers.