完成端口(IOCP) 也可以这样用~~呵呵~ 用来充当线程之间的数据队列~

完成端口貌似就是用 PostQueuedCompletionStatus/GetQueuedCompletionStatus 来管理一个事件队列的~ 即使没有 文件和Socket IO,我们单单用他来管理 线程之间的数据传递,貌似也很好用啊。。。。 ;p

//  Test32.cpp : 定义控制台应用程序的入口点。
//

#include 
" stdafx.h "
#include 
< stdlib.h >
#include 
< windows.h >
#include 
< process.h >
#include 
< iostream >

HANDLE m_hCompletionPort;


volatile  BOOL m_bShutDown;

struct  TESTOVERLAPPED
{
    OVERLAPPED    m_ol;
    CHAR        szData[
512];
}
;


UINT WINAPI IOWorkerThreadProc(LPVOID pParam)
{
    BOOL            bError            
= FALSE;
    DWORD            dwIoSize        
= 0;
    DWORD            dwCmpKey        
= 0;
    LPOVERLAPPED    lpOverlapped    
= NULL;
    TESTOVERLAPPED
*    pOverlapBuff    = NULL;


    
while ( !bError )
    
{
        pOverlapBuff        
= NULL;
        dwCmpKey 
= 0;
        
// Get a completed IO request.
        BOOL bIORet = GetQueuedCompletionStatus(m_hCompletionPort, &dwIoSize, (LPDWORD) &dwCmpKey, &lpOverlapped, INFINITE);
        
// Simulate workload (for debugging, to find possible reordering)
        
//Sleep(20);


        
// If Something whent wrong..
        if (!bIORet)  
        
{
            DWORD dwIOError 
= GetLastError();
            
if(dwIOError != WAIT_TIMEOUT) // It was not an Time out event we wait for ever (INFINITE) 
            {    
    
                    
continue;

            }

        }
// if (!bIORet) 



        
if(bIORet && lpOverlapped && dwCmpKey) 
        
{
            pOverlapBuff
=CONTAINING_RECORD(lpOverlapped, TESTOVERLAPPED, m_ol);
            
if(pOverlapBuff!=NULL)
            
{
                
//pThis->ProcessIOMessage(pOverlapBuff, dwCmpKey, dwIoSize);
                printf("ThreaId:0x%x %d %s ",GetCurrentThreadId(),dwCmpKey,pOverlapBuff->szData);
            }


            delete pOverlapBuff;
        }
    

        
if((dwCmpKey == NULL) && (pOverlapBuff == NULL) && m_bShutDown)
        
{
            bError 
= TRUE;
        }

    }
 

    printf(
"Thread dead!");

    
return 0xdead;
}





int  _tmain( int  argc, _TCHAR *  argv[])
{
    m_hCompletionPort 
= CreateIoCompletionPort( INVALID_HANDLE_VALUE, NULL, 00 );

    uintptr_t hThread 
=_beginthreadex(0,0,IOWorkerThreadProc, 0,0, NULL);
    hThread 
=_beginthreadex(0,0,IOWorkerThreadProc, 0,0, NULL);
    hThread 
=_beginthreadex(0,0,IOWorkerThreadProc, 0,0, NULL);

    m_bShutDown 
= FALSE;

    
for(int i = 0; i < 30; i++)
    
{
        DWORD dwCmpKey 
= i;
        TESTOVERLAPPED 
* pOverlapBuff = new TESTOVERLAPPED;
        memset(pOverlapBuff
->szData,0,sizeof(pOverlapBuff->szData));
        sprintf(pOverlapBuff
->szData,"CMD ID:%d",i);
        PostQueuedCompletionStatus(m_hCompletionPort, 
0, (ULONG_PTR) &dwCmpKey, &pOverlapBuff->m_ol);
    }


    m_bShutDown 
= TRUE;
    PostQueuedCompletionStatus(m_hCompletionPort,
0,0,0);

    getchar();

    
return 0;
}





输出:
ThreaId: 0x890    1310516  CMD ID: 0
ThreaId:
0x890    1310516  CMD ID: 3
ThreaId:
0xa18    1310516  CMD ID: 2
ThreaId:
0xc90    1310516  CMD ID: 1
ThreaId:
0xc90    1310516  CMD ID: 6
ThreaId:
0xc90    1310516  CMD ID: 7
ThreaId:
0xc90    1310516  CMD ID: 8
ThreaId:
0xc90    1310516  CMD ID: 9
ThreaId:
0xc90    1310516  CMD ID: 10
ThreaId:
0xc90    1310516  CMD ID: 11
ThreaId:
0xa18    1310516  CMD ID: 5
ThreaId:
0xa18    1310516  CMD ID: 13
ThreaId:
0xa18    1310516  CMD ID: 14
ThreaId:
0xa18    1310516  CMD ID: 15
ThreaId:
0xa18    1310516  CMD ID: 16
ThreaId:
0xa18    1310516  CMD ID: 17
ThreaId:
0xa18    1310516  CMD ID: 18
ThreaId:
0xa18    1310516  CMD ID: 19
ThreaId:
0xa18    1310516  CMD ID: 20
ThreaId:
0xa18    1310516  CMD ID: 21
ThreaId:
0xa18    1310516  CMD ID: 22
ThreaId:
0xa18    1310516  CMD ID: 23
ThreaId:
0x890    1310516  CMD ID: 4
ThreaId:
0xc90    1310516  CMD ID: 12
ThreaId:
0xa18    1310516  CMD ID: 24
ThreaId:
0xa18    1310516  CMD ID: 27
ThreaId:
0xc90    1310516  CMD ID: 26
ThreaId:
0x890    1310516  CMD ID: 25
ThreaId:
0xa18    1310516  CMD ID: 28
ThreaId:
0xc90    1310516  CMD ID: 29
Thread dead
!

你可能感兴趣的:(完成端口(IOCP) 也可以这样用~~呵呵~ 用来充当线程之间的数据队列~)