IOCP 是否创建自己的线程?

Does IOCP creates its own threads?

我正在学习 IOCP,根据这个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.

我认为唯一存在的线程是我创建的用于从完成端口中取出完成数据包的线程。完成端口创建的这个 "concurrent threads" 是什么?

作者似乎没有完全理解完成端口。除了您引用的声明之外,该文章中还有一个图表再次包含短语 "concurrent threads"。哪有这回事。完成端口的线程数设置是允许同时执行的线程数。

那篇文章还有其他奇怪的地方。他正在创建一个仅用于调用 CreateIoCompletionPort 的套接字。在他破坏插座后立即。作者似乎相信他在网络其他地方看到的一些魔法咒语。不要从字面上理解这篇文章。阅读 API 文档。

IOCP 不会创建自己的线程。后台组中的 IOCP 操作 运行,然后 post 将它们的结果放入 IOCP 完成队列。 CreateIoCompletionPort()NumberOfConcurrentThreads参数只是控制允许多少工作线程同时处理完成数据包。这在 MSDN 文档中有解释。

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.