(Reproduced) Asynchronous IO, APC, IO Complete port, thread pool and high performance server (3)

xiaoxiao2021-03-06  84

Reprinted: http://www.vchelp.net/ The original author's name fang (fangguicheng@21cn.com) Text asynchronous IO, APC, IO completion port, thread pool and high performance server three IO completion port IO completion port below extracted MSDN "I / O Completion Ports", Smallfool Translation, Original Please refer to the 9CBS Document Center Article "I / O Completion Ports", http://dev.9cbs.net/develop/article/29\29240.shtm. I / O completion port is a mechanism. Through this mechanism, the application will first create a thread pool at startup, then the application uses the thread pool to process asynchronous I / O requests. The unique purpose of these threads is used to process I / O requests. For applications that handle a large number of concurrent actions I / O requests, it is faster and more efficient to use the completion port (S) compared to the I / O request to occur. The CreateioCompletionPort function enables an I / O completion port with one or more file handles. When the asynchronous I / O operation started on the file handle associated with a completed port, an I / O completion package will enter the queue of the completion port. For multiple file handles, this mechanism can be used to place the synchronization of the multi-file handle in a single object. (Words, if we need to synchronize each handle file, in general, we need multiple objects (such as: Event to synchronize), and we use the IO Complete Port to implement asynchronous operation, we can use multiple files Configured, whenever the asynchronous operation in a file is completed, putting a Complete Package in the queue so we can use this to complete the synchronization of all file handles.) Call the getQueuedCompletionStatus function, a thread will wait a completion The package enters the queue that completed the port instead of waiting for the asynchronous I / O request to complete. Threads (we) will blocked their running ports (released in order to first out queue order). This means that when a completion package enters the queue of the completed port, the system will release threads recently blocked in the completion port. Calling getQueuedCompletionStatus, the thread will establish a connection with a specified completion port, continuing the existence period of the thread, or a different completion port, or release contact with the completion port. A thread can only be contacted with a completed port that does not exceed one. Completing the most important features of the port is concurrency. Completing the port and emissions can be specified when the completion port is created. The concurrent amount limits the number of running threads associated with the completion port. When the total number of running threads associated with the completion port reaches the sum, the system will block any subsequent threads associated with the completion port until the number of operational thread associated with the completion port is lowered. To smaller than the concurrent release. The most effective imaginary is that there is a completion package waiting in the queue without waiting to be satisfied, because the completion of the port reaches the limit of its concurrency. At this point, when a running thread calls getQueuedCompletionStatus, it will immediately take the completion package from the queue. This does not exist environmental switching, because the running thread will continue to complete the package from the queue, while other threads cannot run. The best choice for concurrency is the number of CPUs in your computer. If your transaction requires a long computation time, a relatively large concurrency can allow more threads to run. Although it takes longer to complete each transaction, more transactions can be handled simultaneously. For applications, it is easy to get the best effect by testing and launched.

The PostQueuedCompletionStatus function allows applications to queue in a custom private I / O complete package without launching an asynchronous I / O operation. This is useful for notifying external events. When there is no more reference to a completed port, you need to release the completion port. This completed port handle and all file handles associated with the completion port need to be released. Calling CloseHandle can release the handle of the completion port. The following code uses IO to complete the port to make a simple thread pool.

/ ************************************************** ********************** / / * TEST IOCOMPLETEPORT. * // ******************** *********************************************************** **** / DWORD WINAPI IOCPWorkThread (PVOID pParam) {HANDLE CompletePort = (HANDLE) pParam; PVOID UserParam; WORK_ITEM_PROC UserProc; LPOVERLAPPED pOverlapped; for (;;) {BOOL bRet = GetQueuedCompletionStatus (CompletePort, (LPDWORD) & UserParam, (LPDWORD ) & UserProc, & pOverlapped, INFINITE); _ASSERT (bRet);. if (UserProc == NULL) // Quit signal break;. // execute user's proc UserProc (UserParam);} return 0;} void TestIOCompletePort (BOOL bWaitMode, LONG Threadnum) {Handle CompletePort; overlapped overlapped = {0, 0, 0, 0, null}; completion = creteiocompletionport (Invalid_Handle_Value, Null, NULL, 0); // Create threads for (int i = 0; i

转载请注明原文地址:https://www.9cbs.com/read-94943.html

New Post(0)