1

I am learning about IOCP, and according to this article:

To begin using completion ports we need to create a Completion Port which in turn creates a number of concurrent threads (threads that exist with the Completion Port - Not to be confused with Worker Threads) that you specify.

I thought that the only threads that exists are the ones that I create to dequeue the completion packets from the completion port. What is this "concurrent threads" that the completion port creates?

2 Answers2

10

The author does not seem to fully understand completion ports. In addition to the statement you quoted there is a diagram in that article containing the phrase "concurrent threads" again. There is no such thing. The number of threads setting of a completion port is the number of threads that will be allowed to execute simultaneously.

There are other strange things in that article. He's creating a socket just for calling CreateIoCompletionPort. Immediately after he's destroying the socket. The author seems to believe in a few magic incantations that he has seen elsewhere on the web. Don't take this article too literally. Read the API docs.

usr
  • 168,620
  • 35
  • 240
  • 369
4

An IOCP does not create its own threads. IOCP operations run in the backgroup and then post their result to the IOCP completion queue. The NumberOfConcurrentThreads parameter of CreateIoCompletionPort() merely controls how many worker threads are allowed to process completion packets at the same time. This is explained in the MSDN documentation.

I/O Completion Ports

Although any number of threads can call GetQueuedCompletionStatus for a specified I/O completion port, when a specified thread calls GetQueuedCompletionStatus the first time, it becomes associated with the specified I/O completion port until one of three things occurs: The thread exits, specifies a different I/O completion port, or closes the I/O completion port. In other words, a single thread can be associated with, at most, one I/O completion port.

When a completion packet is queued to an I/O completion port, the system first checks how many threads associated with that port are running. If the number of threads running is less than the concurrency value (discussed in the next section), one of the waiting threads (the most recent one) is allowed to process the completion packet. When a running thread completes its processing, it typically calls GetQueuedCompletionStatus again, at which point it either returns with the next completion packet or waits if the queue is empty.

...

The most important property of an I/O completion port to consider carefully is the concurrency value. The concurrency value of a completion port is specified when it is created with CreateIoCompletionPort via the NumberOfConcurrentThreads parameter. This value limits the number of runnable threads associated with the completion port. When the total number of runnable threads associated with the completion port reaches the concurrency value, the system blocks the execution of any subsequent threads associated with that completion port until the number of runnable threads drops below the concurrency value.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770