CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2010
    Posts
    9

    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.

    In particular, can anyone clarify what the following passage means?

    (from http://msdn.microsoft.com/en-us/libr...58(VS.85).aspx)

    "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 :-)

  2. #2
    Join Date
    Nov 2003
    Posts
    1,902

    Re: confused about overlapped I/O and Events

    Looks like an error in the documentation. Change that "TRUE" to "FALSE" and the paragraph makes sense.

    gg

  3. #3
    Join Date
    Sep 2010
    Posts
    9

    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.

    Does this sound right?

  4. #4
    Join Date
    Nov 2003
    Posts
    1,902

    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.

    gg

  5. #5
    Join Date
    Sep 2010
    Posts
    9

    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured