http://blog.sina.com.cn/s/blog_47642c6e0102w1se.html
I/O Completion Ports
I/O完成端口
I/O completion ports provide an efficient threading model for processing multiple asynchronous I/O requests on a multiprocessor system. When a process creates an I/O completion port, the system creates an associated queue object for requests whose sole purpose is to service these requests. Processes that handle many concurrent asynchronous I/O requests can do so more quickly and efficiently by using I/O completion ports in conjunction with a pre-allocated thread pool than by creating threads at the time they receive an I/O request.
I/O完成端口为多处理器系统上处理多种异步请求提供了一个有效的线程模型。当进程创建一个I/O完成端口时,系统会为这些请求创建一个关联的队列对象,此对象只服务于这些请求。相比在接收到I/O请求时创建线程,同时使用I/O完成端口和预分配的线程池,处理大并发异步I/O请求的进程可以更加快速高效。
How I/O Completion Ports Work
I/O完成端口如何工作
The CreateIoCompletionPort function creates an I/O completion port and associates one or more file handles with that port. When an asynchronous I/O operation on one of these file handles completes, an I/O completion packet is queued in first-in-first-out (FIFO) order to the associated I/O completion port. One powerful use for this mechanism is to combine the synchronization point for multiple file handles into a single object, although there are also other useful applications. Please note that while the packets are queued in FIFO order they may be dequeued in a different order.
CreateIoCompletionPort函数可以创建一个完成端口,并且可以在此端口上关联一个或多个文件句柄。当这些文件句柄中的某个句柄上的一个异步I/O操作完成时,一个I/O完成包会按先进先出顺序加入关联的I/O完成端口队列。这种机制的一种强大应用是把多个文件句柄的同步点放入一个对象,当然也有其他有用的用法。请注意当包按先进先出顺序加入队列时,它们可能按不同的顺序被取出队列。
Note
注意
The term file handleas used here refers to a system abstraction representing an overlapped I/O endpoint, not only a file on disk. For example, it can be a network endpoint, TCP socket, named pipe, or mail slot. Any system object that supports overlapped I/O can be used. For a list of related I/O functions, see the end of this topic.
这里所使用的术语“file handles”指的是一个系统抽象概念,表示一个重叠I/O终结点,而不仅仅指磁盘上的一个文件。例如,它可以是一个网络终结点,TCP套接字,命名管道,或者邮件槽。任何支持重叠I/O的系统对象都可以在这里使用。要查看相关I/O函数列表,请转到此话题结尾。
When a file handle is associated with a completion port, the status block passed in will not be updated until the packet is removed from the completion port. The only exception is if the original operation returns synchronously with an error. A thread (either one created by the main thread or the main thread itself) uses the GetQueuedCompletionStatu
当一个文件句柄被关联到一个完成端口,输入的状态信息结构只有在I/O完成包从完成端口队列中移除时才会更新。仅有的异常是否原始操作会出现错误并同步返回。一个线程(主线程或主线程创建的线程)可以使用GetQueuedCompletionStatu
Although any number of threads can call GetQueuedCompletionStatu
虽然任何数量的线程都可以在一个指定的I/O完成端口上调用GetQueuedCompletionStatu
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 GetQueuedCompletionStatu
当一个完成包被加入I/O完成端口的队列,系统首先检查关联到此端口的线程有多少正在运行。如果线程数量比并发值少(在下一章讨论),其中一个等待的线程(最近一个)将被允许处理此完成包。当一个运行中的线程完成它的处理过程,正常情况下它会再次调用GetQueuedCompletionStatu
Threads can use the PostQueuedCompletionStat
线程可以使用PostQueuedCompletionStat
The I/O completion port handle and every file handle associated with that particular I/O completion port are known as references to the I/O completion port. The I/O completion port is released when there are no more references to it. Therefore, all of these handles must be properly closed to release the I/O completion port and its associated system resources. After these conditions are satisfied, an application should close the I/O completion port handle by calling the CloseHandle function.
I/O完成端口句柄和每个关联到此特定I/O完成端口的文件句柄被看作对I/O完成端口的引用。在没有引用时I/O完成端口才会被释放。因此,所有这些句柄必须恰当地关闭,这样才能释放I/O完成端口和它关联的系统资源。当这些情况满足后,应用程序应当调用CloseHandle函数关闭I/O完成端口句柄。
Note
注意
An I/O completion port is associated with the process that created it and is not sharable between processes. However, a single handle is sharable between threads in the same process.
一个I/O完成端口会关联到创建它的进程,但并不能在进程间共享。当然,在同一个进程的线程间单个句柄是可以共享的。
Threads and Concurrency
线程和并发
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 withCreateIoCompletionPort via the NumberOfConcurrentThread
需要考虑的I/O完成端口最重要的属性是并发值。一个完成端口的并发值在它被CreateIoCompletionPort创建时通过NumberOfConcurrentThread
The most efficient scenario occurs when there are completion packets waiting in the queue, but no waits can be satisfied because the port has reached its concurrency limit. Consider what happens with a concurrency value of one and multiple threads waiting in the GetQueuedCompletionStatu
最高效的场景出现在,队列中有完成包等待处理,但因为端口已经达到它的并发上限无法满足等待条件。想一想并发值这种数量的一个或多个线程正在等待调用GetQueuedCompletionStatu
Note
注意
In the previous example, the extra threads appear to be useless and never run, but that assumes that the running thread never gets put in a wait state by some other mechanism, terminates, or otherwise closes its associated I/O completion port. Consider all such thread execution ramifications when designing the application.
在前面的例子中,额外的线程好像看起来没什么用,并且一直没有运行,但这是假定运行中的线程从没有被一些其他机制置为等待状态,终止,或者关闭它关联的I/O完成端口。当设计程序时应当考虑所有这些线程执行流程。
The best overall maximum value to pick for the concurrency value is the number of CPUs on the computer. If your transaction required a lengthy computation, a larger concurrency value will allow more threads to run. Each completion packet may take longer to finish, but more completion packets will be processed at the same time. You can experiment with the concurrency value in conjunction with profiling tools to achieve the best effect for your application.
作为并发值的最佳的总体最大值是计算机中央处理器的数量。如果你的事务需要长时间运算,设置一个更大的并发值可以允许更多的线程运行。每一个完成包或许需要长时间来完成,但更多的完成包会在同时处理。你可以结合性能分析工具来为你的程序试验出一个最高效的并发值。
The system also allows a thread waiting in GetQueuedCompletionStatu
系统也允许一个线程等待GetQueuedCompletionStatu
Supported I/O Functions
支持的I/O函数
The following functions can be used to start I/O operations that complete by using I/O completion ports. You must pass the function an instance of the OVERLAPPED structure and a file handle previously associated with an I/O completion port (by a call to CreateIoCompletionPort) to enable the I/O completion port mechanism:
以下函数可以被用于启动一个使用I/O完成端口完成的I/O操作。你可以给函数传递一个OVERLAPPED结构的实例和一个已经提前关联到一个I/O完成端口的文件句柄(通过调用CreateIoCompletionPort)来启用I/O完成端口机制:
·
·
·