Windows API One Day Practice (CreateIoCompletionPort) and GetQueuedCompletionStatus function __ function

Source: Internet
Author: User
Tags message queue
In Windows systems, using a completion port is one of the high-performance methods, such as using the completion port to the thread pool and the network server. Now it's time to introduce how to use the completion port through the thread pool method, and then carefully describe how to construct the server. In fact, the completion port is a queue, all threads are waiting for messages to appear, if there is a message in the queue, each thread to get a message to execute it. Use the function CreateIoCompletionPort to create a message queue, and then use the GetQueuedCompletionStatus function to get the message from the queue, using the function postqueuedcompletionstatus To send a message to the queue. The three functions are used to implement the message loop processing of the completion port.   Functions CreateIoCompletionPort, GetQueuedCompletionStatus, PostQueuedCompletionStatus declared as follows:   WINBASEAPI __out H Andle WINAPI CreateIoCompletionPort (    __in     HANDLE filehandle,   & nbsp __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 & nbsp;  ); FileHandleis the associated file handle. ExistingcompletionportIs the completion port that already exists. CompletionkeyIs the parameter that is passed to the processing function. NumberOfConcurrentThreadsIs how many threads are accessing this message queue. CompletionportIs the completion port that already exists. lpCompletionKeyIs the parameter that is passed to the processing function. lpoverlappedIs the parameter that is passed to the processing function. dwmillisecondsIs the waiting time. dwnumberofbytestransferredIs how many bytes were transferred. The example of calling a function is as follows: #001 #pragma once #002 #003 #include "Thread.h" #004 #005//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 a IOCP. #024 m_hqueue = CreateIoCompletionPort (Invalid_handle_value, NULL, 0, m_nthreadcount); #025          if (m_hqueue = NULL) #026           {#027               //Create IOCP Failed. #028                return false; #029         } #030  &nbsp} #031   #032   int Getthreadcount (void) const #033   {#034          return m_ Nthreadcount; #035  &nbsp} #036   #037 the content that is 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 a IOCP message. #046         while (GetQueuedCompletionStatus M_hqueue, & Dwbytestransfered, &dwcompletionkey, &poverlapped, INFINITE)) #047          {#048                 if (poverlapped = (overlapped*) ((__int64)-1)) #049     & nbsp;          {#050                     //exit. #051                      OutputDebugString (_t ("Exit/r/n")); #052                      break; #053               } #054                 else                                               #055          #056                 {#057                      WPARAM request = (WPARAM) Dwcompletionkey; #058   #059                     //processing of messages. #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  &nbsp} Closes all threads #078   #079   //. #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  &nbsp #087   #088  protected: #089    //a queue that receives message processing. #090   handle M_hqueue; Number of threads #091   #092   //. #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  &nbsp} #105   virtual ~cthreads (void) #106   {#107   #108 &NBSP;&NBSP} #109   #110   #111  protected: #112   //#113   //threads run functions. #114   //can use members of a class here, or you can make derived classes 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  &nbsp} #126   #127  protected: #128   cthreadpools* m_ppool; #129   #130  };  

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.