c++ timer基於win訊息佇列

來源:互聯網
上載者:User

可以承載10w個timer通訊運行,說關閉就關閉,裡面用了一個比較巧妙的線程處理,呵呵10W個timer就10多個線程,請大牛不要笑話,供新手學習之用

 

 

#pragma once

#include <Windows.h>

 

typedef void (CALLBACK* UXTIMERCALLBACK)(DWORD,void*);

 

#include <map>

 

#define G_UXTimerQueue (CUXTimer::GetInstance())

 

//--------------------------------------------------------------------------------------

typedef struct tegTIMERINFO 

{

HANDLEhEvent;

DWORDdwEventID;

UXTIMERCALLBACKcallback;

void*pEvent;

void*pThis;

HANDLEhCmpEvent;

}TIMERINFO ;

 

typedef std::map<DWORD,TIMERINFO*> TIMERQUEUE;

 

//-------------------------------------------------------------------------------------

class CUXTimer

{

public:

CUXTimer();

virtual ~CUXTimer();

void SetTimer(DWORD dwIdEvent,DWORD dwTime,UXTIMERCALLBACK callBack,void* p);

void KillTimer(DWORD dwIdEvent);

 

static CUXTimer& GetInstance()

{

static CUXTimer u;

return u;

}

 

inline TIMERQUEUE& GetTimerQueue()

{

return m_timer_queue;

};

inline HANDLE GetTimerQueueHandle()

{

return m_hTimerQueue;

};

 

inline DWORD GetIdEvent()

{

InterlockedIncrement((LONG*)&m_longIdEvent);

if(m_longIdEvent==UINT_MAX)

m_longIdEvent = 1;

return (DWORD)m_longIdEvent;

}

protected:

private:

HANDLEm_hTimerQueue;

TIMERQUEUEm_timer_queue;

HANDLEm_hGuardThd;

 

private:

unsigned long m_longIdEvent;

 static void WINAPI TimerFunc(void*,bool);

static unsigned int __stdcall GuardThd(PVOID p);

public:

CRITICAL_SECTION m_cs_timer_queue;

static CUXTimer* m_pThis;

};

//--------------------------------------------------------------------------------------

 

 

//cpp

 

#include "stdafx.h"

#include "UXTimerQueue.h"

#include <process.h>

 

CUXTimer* CUXTimer::m_pThis = NULL;

//--------------------------------------------------------------------------------------------------------

CUXTimer::CUXTimer():m_longIdEvent(0)

{

m_hTimerQueue = CreateTimerQueue();

InitializeCriticalSection(&m_cs_timer_queue);

m_pThis = this;

m_hGuardThd = (HANDLE)_beginthreadex(NULL,0,GuardThd,0,0,0);

}

//--------------------------------------------------------------------------------------------------------

CUXTimer:: ~CUXTimer()

{

DeleteTimerQueue(m_hTimerQueue);

DeleteCriticalSection(&m_cs_timer_queue);

m_timer_queue.clear();

 

}

//--------------------------------------------------------------------------------------------------------

void CUXTimer::SetTimer(DWORD dwIdEvent,DWORD dwTime,UXTIMERCALLBACK callBack,void* p)

{

TIMERQUEUE::iterator it;

EnterCriticalSection(&m_cs_timer_queue);

TIMERINFO* t=new TIMERINFO;

t->dwEventID = dwIdEvent;

t->callback = callBack;

t->pEvent = p;

t->pThis = this;

t->hCmpEvent = CreateEvent(NULL,1,1,0);

ResetEvent(t->hCmpEvent);

if(!CreateTimerQueueTimer(&t->hEvent,m_hTimerQueue,(WAITORTIMERCALLBACK)TimerFunc,(void*)t->dwEventID,dwTime,dwTime,WT_EXECUTEINIOTHREAD))

{

if (!t->hEvent)

{

DeleteTimerQueueTimer(m_hTimerQueue,t->hEvent,t->hCmpEvent);

if (t)

{

delete t;

t = NULL;

}

LeaveCriticalSection(&m_cs_timer_queue);

return;

}

}

//預防在維護線程沒有清空原有的timer,這時候這個timer重複利用,需要釋放先前一個timer

if ( (it = m_timer_queue.find(dwIdEvent) ) != m_timer_queue.end())

{

if (it->second)

{

delete it->second;

it->second = NULL;

}

m_timer_queue.erase(it);

}

//將timer添加到map中用於儲存

m_timer_queue.insert(std::make_pair(dwIdEvent,t));

LeaveCriticalSection(&m_cs_timer_queue);

}

//--------------------------------------------------------------------------------------------------------

void CUXTimer::KillTimer(DWORD dwIdEvent)

{

EnterCriticalSection(&m_cs_timer_queue);

TIMERQUEUE::iterator it = m_timer_queue.find(dwIdEvent);

if (it!=m_timer_queue.end())

{

it->second->pThis = NULL;

if (it->second->hEvent)

{

DeleteTimerQueueTimer(m_hTimerQueue,it->second->hEvent,it->second->hCmpEvent);

OutputDebugStringA("KILLTIMER/n");

}

 

}

LeaveCriticalSection(&m_cs_timer_queue);

 

}

//--------------------------------------------------------------------------------------------------------

void  CUXTimer::TimerFunc(void* p,bool b)

{

EnterCriticalSection(&G_UXTimerQueue.m_cs_timer_queue);

DWORD dwEventID = (DWORD)p;

TIMERQUEUE::iterator it1 = m_pThis->m_timer_queue.find(dwEventID);

if (it1 != m_pThis->m_timer_queue.end())

{

if (it1->second->pThis)

{

it1->second->callback(dwEventID,it1->second->pEvent);

}

 

}

LeaveCriticalSection(&G_UXTimerQueue.m_cs_timer_queue);

}

//--------------------------------------------------------------------------------------------------------

 

unsigned int __stdcall CUXTimer::GuardThd(PVOID p)

 {

while(1)

{

if (m_pThis->m_timer_queue.size()>0)

{

EnterCriticalSection(&G_UXTimerQueue.m_cs_timer_queue);

for (TIMERQUEUE::iterator it = m_pThis->m_timer_queue.begin();it != m_pThis->m_timer_queue.end();)

{

if(WaitForSingleObject(it->second->hCmpEvent,0) == WAIT_OBJECT_0)

{

CloseHandle(it->second->hCmpEvent);

it->second->hCmpEvent = NULL;

 

if (it->second)

{

delete it->second;

it->second = NULL;

}

 

m_pThis->m_timer_queue.erase(it++);

OutputDebugStringA("REAL_KILLTIMER/n");

}

else

it++;

}

LeaveCriticalSection(&G_UXTimerQueue.m_cs_timer_queue);

}

 

Sleep(500);

}

 

return 0;

 }

 

 //--------------------------------------------------------------------------------------------------------

 

//使用方法

 

int i = 0;

for (i = 0;i<10000;i++)

{

G_UXTimerQueue.SetTimer(i,500,timer,(void*)i); //啟動

}

 

Sleep(10000);

 

i = 0;

for (;i<10000;i++)

{

G_UXTimerQueue.KillTimer(i); //停止

}

 

Sleep(10000);

 

for (i = 0;i<10000;i++)

{

G_UXTimerQueue.SetTimer(i,500,timer,(void*)i);

}

 

Sleep(10000);

 

i = 0;

for (;i<10000;i++)

{

G_UXTimerQueue.KillTimer(i);

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.