1

I need to develop an application that uses IOCP in a UDP socket, but the material found both in the Microsoft documentation as other examples or are vague or focus in the form of implementation. I would like someone with experience in the use of IOCP confirm that the correct usage is:

  • Call CreateIoCompletionPort function to create an IOCP.
  • I associate my socket to my IOCP with the same function.
  • Perform an IO operation (WSARecvFrom or WSASendTo in my case)
  • Call GetQueuedCompletionStatus function that will block my process until my IO operation has completed (this can be done in a thread pool for example)
  • Realize the reading of the buffer or the operation result.

I have not described the implementations of the socket because it is not the focus, but is this the correct way to work with IOCPs?

A second question is about the application design, in the case of sending information. I read something that said that the application that works with IOCPs would not use direct calls to the output operations (such as call directly WSASendTo function), but use the PostQueuedCompletionStatus function to generate an event in the thread to run this operation. According to what I've studied, I don't see any advantage in terms of performance.

Assuming that both forms are applied to a thread pool, there is advantage in using PostQueuedCompletionStatus to perform the output operation?

Victor França
  • 306
  • 4
  • 15
  • Microsoft documentation on [I/O Completion Ports](https://msdn.microsoft.com/en-us/library/aa365198(VS.85).aspx) has a number of links to succeeding topics. Interesting in that it appears you can use an I/O Completion Port to queue messages between threads without any actual I/O by using the `GetQueuedCompletionStatus()` function to wait for a message in one thread and the `PostQueuedCompletionStatus ()` function in the thread sending a message. – Richard Chambers Jun 15 '16 at 00:43
  • 1
    See the answer for [Sockets using GetQueuedCompletionStatus and ERROR_MORE_DATA](http://stackoverflow.com/questions/31883438/sockets-using-getqueuedcompletionstatus-and-error-more-data). A question [Using IOCP with UDP?](http://stackoverflow.com/questions/11361208/using-iocp-with-udp) has a nice looking discussion. – Richard Chambers Jun 15 '16 at 00:50
  • 1
    "*is this the correct way to work with IOCPs?*" - it is *one* way to work with IOCPs, yes. "*use the PostQueuedCompletionStatus function to generate an event in the thread to run [an] operation*" - not a common way to do it, and offers no real benefit. There is nothing wrong with issuing a direct I/O operation at the time you need to perform it. – Remy Lebeau Jun 15 '16 at 01:48
  • Before Windows Vista, any outstanding overlapped I/O operations that were initiated by a thread would be cancelled when that thread exited. To avoid this causing problems you could instead force I/O operations to be executed on a thread that you controlled the lifetime of by first using a PostQueuedCompletionStatus() call to transfer a 'request' to issue an I/O request to your I/O thread pool. This kind of behaviour is no longer required as from Windows Vista onwards pending I/O requests are no longer cancelled when the issuing thread exits. It was an esoteric design that's no longer needed. – Len Holgate Jun 20 '16 at 08:41
  • 1
    @LenHolgate there is also the problem of potential synchronous completion, which is often undesirable on a thread that generally has better things to do. – nfries88 Mar 18 '23 at 01:10

0 Answers0