One-day Windows API training (60) CreateIoCompletionPort and GetQueuedCompletionStatus Functions

Source: Internet
Author: User

In Windows, using the completed port is one of the high-performance methods, such as using the completed port to the thread pool and network server. Now we will introduce how to use the finished port through the thread pool method, and how to construct a high-performance server later. In fact, the completion port is a queue, and all threads are waiting for messages to appear. If there is a message in the queue, each thread obtains a message to execute it. Use the CreateIoCompletionPort function to create a message queue, and then use the GetQueuedCompletionStatus function to obtain messages from the queue. Use the PostQueuedCompletionStatus function to send messages to the queue. The three functions are used to implement the message loop processing on the port.
 
The CreateIoCompletionPort, GetQueuedCompletionStatus, and PostQueuedCompletionStatus functions are declared as follows:
 
WINBASEAPI
_ Out
HANDLE
WINAPI
CreateIoCompletionPort (
_ In HANDLE FileHandle,
_ In_opt HANDLE ExistingCompletionPort,
_ In ULONG_PTR CompletionKey,
_ In DWORD NumberOfConcurrentThreads
);
WINBASEAPI
BOOL
WINAPI
GetQueuedCompletionStatus (
_ In HANDLE CompletionPort,
_ Out LPDWORD lpNumberOfBytesTransferred,
_ Out PULONG_PTR lpCompletionKey,
_ Out LPOVERLAPPED * lpOverlapped,
_ In DWORD dwMilliseconds
);
 
WINBASEAPI
BOOL
WINAPI
PostQueuedCompletionStatus (
_ In HANDLE CompletionPort,
_ In DWORD dwNumberOfBytesTransferred,
_ In ULONG_PTR dwCompletionKey,
_ In_opt LPOVERLAPPED lpOverlapped
);
FileHandle is the associated file handle.
ExistingCompletionPort is an existing completion port.
CompletionKey is a parameter sent to the processing function.
NumberOfConcurrentThreads is how many threads are accessing this message queue.
CompletionPort is an existing completion port.
LpCompletionKey is a parameter sent to the processing function.
LpOverlapped is a parameter sent to the handler.
DwMilliseconds is the wait time.
DwNumberOfBytesTransferred indicates the number of bytes transferred.
 
An example of calling a function is as follows:
#001 # pragma once
#002
#003 # include "Thread. h"
#004
#005
#006 // use IOCP to implement the thread pool.
#007 // Cai junsheng 2007/10/29 QQ: 9073204 Shenzhen
#008 class CThreadPools
#009 {
#010 public:
#011
#012 CThreadPools (void)
#013 {
#014 m_nThreadCount = 2;
#015}
#016
#017 ~ CThreadPools (void)
#018 {
#019}
#020
#021 bool Init (void)
#022 {
#023 // create an IOCP.
#024 m_hQueue = CreateIoCompletionPort (INVALID_HANDLE_VALUE, NULL, 0, m_nThreadCount );
#025 if (m_hQueue = NULL)
#026 {
#027 // An error occurred while creating the IOCP.
#028 return false;
#029}
#030}
#031
#032 int GetThreadCount (void) const
#033 {
#034 return m_nThreadCount;
#035}
#036
#037 // content processed by the thread pool.
#038 DWORD Run (void)
#039 {
#040 DWORD dwBytesTransfered;
#041 ULONG_PTR dwCompletionKey;
#042
#043 OVERLAPPED * pOverlapped;
#044
#045 // wait for an IOCP message.
#046 while (GetQueuedCompletionStatus (m_hQueue, & dwBytesTransfered, & dwCompletionKey, & pOverlapped, INFINITE ))
#047 {
#048 if (pOverlapped = (OVERLAPPED *) (_ int64)-1 )))
#049 {
#050 // exit.
#051 OutputDebugString (_ T ("Exit \ r \ n "));
#052 break;
#053}
#054 else
#055
#056 {
#057 WPARAM request = (WPARAM) dwCompletionKey;
#058
#059 // process the message.
#060 OutputDebugString (_ T ("GetQueuedCompletionStatus \ r \ n "));
#061}
#062}
#063
#064 return 0;
#065}
#066
#067 // send the processed message.
#068 bool QueueRequest (WPARAM wParam)
#069 {
#070 // send a message to IOCP.
#071 if (! PostQueuedCompletionStatus (m_hQueue, 0, (ULONG_PTR) wParam, NULL ))
#072 {
#073 return false;
#074}
#075
#076 return true;
#077}
#078
#079 // close all threads.
#080 void Close (void)
#081 {
#082 for (int I = 0; I <m_nThreadCount; I ++)
#083 {
#084 PostQueuedCompletionStatus (m_hQueue, 0, 0, (OVERLAPPED *) (_ int64)-1 ));
#085}
#086}
#087
#088 protected:
#089 // the queue for receiving message processing.
#090 HANDLE m_hQueue;
#091
#092 // Number of threads.
#093 int m_nThreadCount;
#094 };
#095
#096 ////////////////////////////////////// ////////////////////////////////////
#097 class CThreads:
#098 public CThread
#099 {
#100 public:
#101 CThreads (CThreadPools * pPool)
#102 {
#103 m_pPool = pPool;
#104}
#105 virtual ~ CThreads (void)
#106 {
#107
#108}
#109
#110
#111 protected:
#112 //
#113 // run the function in a thread.
#114 // here, you can use the members in the class or make the derived class more powerful.
#115 // Cai junsheng 2007/10/29
#116 virtual DWORD Run (void)
#117 {
#118 //
#119 if (m_pPool)
#120 {
#121 return m_pPool-> Run ();
#122}
#123
#124 return-1;
#125}
#126
#127 protected:
#128 CThreadPools * m_pPool;
#129
#130 };

This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/caimouse/archive/2007/10/29/1855142.aspx

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.