I/O Completion Ports
I/O Completion Ports are the mechanisms by which an application uses a pool of threads that was created when the application was started to process asynchronous I/O requests. these threads are created for the sole purpose of processing I/O requests. applications that process logs concurrent asynchronous I/O requests can do so more quickly and efficiently by using I/O Completion Ports than by using creating threads at the time of the I/O Request.
I/O completion port (s) is a mechanism through which an application will first create a thread pool at startup, the application then uses the thread pool to process asynchronous I/O requests. These threads are created for the sole purpose of processing I/O requests. For applications that process a large number of concurrent asynchronous I/O requests, the completion port (s) is used when a thread is created when an I/O Request occurs) it can be faster and more efficient.
The createiocompletionport function associates an I/O completion port with one or more file handles. when an asynchronous I/O operation started on a file handle associated with a completion port is completed, an I/O completion packet is queued to the port. this can be used to combine the synchronization point for multiple file handles into a single object.
The createiocompletionport function associates an I/O completion port with one or more file handles. When the asynchronous I/O operations started on a file handle related to the completed port are completed, an I/O completed package will enter the queue of the completed port. This mechanism can be used to place synchronization points of multiple file handles in a single object. (The implication is that if we need to synchronize each handle file, we usually need multiple objects (such as event to synchronize ), i/O complete port is used to implement asynchronous operations. We can associate multiple files. Each time an asynchronous operation in a file is completed, a complete package is put into the queue, in this way, we can use this to synchronize all file handles)
A thread uses the getqueuedcompletionstatus function to wait for a completion packet to be queued to the completion port, rather than waiting directly for the asynchronous I/O to complete. threads that block their execution on a completion port are released in last-in-first-out (LIFO) Order. this means that when a completion packet is queued to the completion port, the system releases the last thread to block its execution on the port.
Call the getqueuedcompletionstatus function. A thread waits for a complete package to enter the queue of the completed port, instead of waiting for the asynchronous I/O Request to complete. The threads waiting for the completion of the completion port (of course, it will temporarily stop the execution) will be released in a later, first, and foremost manner. This means that when an asynchronous Io operation is completed, the completion packet will be put into the queue, and the system will release the last waiting thread to process the completion packet.
When a thread CILS getqueuedcompletionstatus, it is associated with the specified completion port until it exits, specifies a different completion port, or frees the completion port. A thread can be associated with at most one completion port.
When getqueuedcompletionstatus is called, the thread will establish a connection with a specified completion port, continuing the existence cycle of the thread, or being specified with a different completion port, or release the connection with the completion port.A thread can only be associated with up to one completion port.
The most important property of a completion port is the concurrency value. the concurrency value of a completion port is specified when the completion port is created. 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 that specify the completion port until the number of runnable threads associated with the completion port drops below the concurrency value. the most efficient scenario occursWhen there are completion packets waiting in the queue, but no waits can be satisfied because the port has reached its concurrency limit.In this case, when a running thread callgetqueuedcompletionstatus, it will immediately pick up the queued completion packet. no context switches will occur, because the running thread is continually picking up completion packets and the other threads are unable to run.
The most important feature of port completion is the concurrency. The concurrency of the completed port can be specified when the completed port is created. This concurrency limit the number of runnable threads associated with the completion port. When the total number of runable threads associated with the completion port reaches this concurrency, the system will block any subsequent thread execution associated with the completion port, until the number of runable threads associated with the port is reduced to less than the number of concurrent threads. The most effective assumption is thatThere are finished packets waiting in the queue, but not waiting to be met, because at this time the completion port reaches the limit of its concurrency. At this time, when a running thread calls getqueuedcompletionstatus, it immediately removes the completion package from the queue. In this way, there is no environment switch, because the running thread will continuously remove the complete package from the queue, and other threads will not be able to run.
The best value to pick for the concurrency value is the number of CPUs on the machine. if your transaction required a lengthy computation, a larger concurrency value will allow more threads to run. each transaction will take longer to complete, but more transactions will be processed at the same time. it is easy to experiment with the concurrency value to achieve the best effect for your application.
The best choice for concurrency is the number of CPUs in your computer. If your transaction processing takes a long computing time, a large amount of concurrency can allow more threads to run. Although it takes longer to complete each transaction, more transactions can be processed at the same time. For applications, it is easy to obtain the best results by testing the concurrency.
The postqueuedcompletionstatus function allows an application to queue its own special-purpose I/O completion packets to the completion port without starting an asynchronous I/O operation. this is useful for policying worker threads of external events.
The postqueuedcompletionstatus function allows applications to queue custom dedicated I/O completion packages without starting an asynchronous I/O operation. This is useful for the worker thread that notifies external events.
The completion port is freed when there are no more references to it. the completion port handle and every file handle associated with the completion port reference the completion port. all the handles must be closed to free the completion port. to close the port handle, call the closehandle function.
When there are no more references for a completed port, you need to release the completed port. The completed port handle and all file handles associated with the completed port need to be released. Call closehandle to release the handle of the completed port.