0

Windows IOCP documentation states this with regard to GetQueuedCompletionStatus:

Threads that block their execution on an I/O completion port are released in last-in-first-out (LIFO) order, and the next completion packet is pulled from the I/O completion port's FIFO queue for that thread.

Consider a scenario with threads T1 and T2:

  1. T1 associates handle H1 with the IOCP followed by T2 associating handle H2 with the same IOCP.
  2. T1 calls GetQueuedCompletionStatus and is blocked.
  3. T2 calls GetQueuedCompletionStatues and is blocked.
  4. The I/O call operation on H1 completes.

Now, given the above statement that the threads "are released in last-in-first-out (LIFO) order", will T1 remain in a wait state till T2 is released to satisfy LIFO ordering?

Agnel Kurian
  • 57,975
  • 43
  • 146
  • 217
  • 1
    That's what LIFO means. – Raymond Chen Mar 08 '23 at 17:46
  • @RaymondChen Thank you for clarifying. I was left wondering what would happen if T2 does not receive it's expected input for a prolonged duration, maybe forever. Now that would cause all other threads to remain in wait state. I take it that this is one of the things we need to consider when using IOCP. (PS: Feels great to hear from **the** Raymond Chen himself!) – Agnel Kurian Mar 08 '23 at 17:51
  • 1
    If T2 does not receive any input for a prolonged duration, then that means that no completions have been posted to the IOCP. So there's no reason to wake up T1 either. Why would you wake up T1 if there is no input for it? – Raymond Chen Mar 08 '23 at 18:05
  • Got it! So threads aren't waiting for input from any particular handle i.e. T1 and T2 can handle input from either H1 or H2 and there is no one-to-one correspondence between the handle and the thread. I hope I have understood correctly. – Agnel Kurian Mar 08 '23 at 18:21
  • 2
    That's the point of an IOCP. Any thread in an IOCP can handle any event posted to the IOCP. If you want T1 to respond only to H1 activity, then create two IOCPs, one exclusively for T1 and H1, and another for T2 and H2. – Raymond Chen Mar 08 '23 at 20:01

0 Answers0