消息队列学习

摘自:http://www.cnblogs.com/jiayongzhi/archive/2011/05/09/2041655.html

#include <iostream>
#include <Windows.h>
using namespace std;

const int MAXMESSAGE = 1024;

class CXMessageList
{
public:
	CXMessageList(void);
	~CXMessageList(void){};
public:
	MSG* GetTopMessage();
	MSG* WaitForMessage();
	int GetMessageCount(){return mCount;};
	int InsertMessage(MSG *msg);
private:
	int mCount;
	MSG *mList[MAXMESSAGE];
	HANDLE mCS_Message;
};
int CXMessageList::InsertMessage( MSG *msg )
{
	if(mCount>MAXMESSAGE)
	{
		return -1;
	}
	mList[mCount++] = msg;
	SetEvent(mCS_Message);
	return 1;
}

MSG* CXMessageList::GetTopMessage()
{
	MSG *pMsg = NULL;
	//ZeroMemory(pMsg, sizeof(MSG));
	if(mCount > 0)
	{
		pMsg = mList[0];
		for(int i = 0; i<mCount-1; i++)
		{
			mList[i] = mList[i+1];
		}
		mList[mCount - 1] = NULL;
		mCount--;
	}
	if(mCount == 0)
	{
		ResetEvent(mCS_Message);
	}
	return pMsg;
}

MSG * CXMessageList::WaitForMessage()
{
	if(mCount>0)
	{
		return GetTopMessage();
	}
	else
	{
		WaitForSingleObject(mCS_Message, INFINITE);
		return GetTopMessage();
	}
}

CXMessageList::CXMessageList( void )
{
	mCount = 0;
	ZeroMemory(&mList, MAXMESSAGE*sizeof(MSG*));
	char CSName[32];
	DWORD ThreadID = GetCurrentThreadId();
	sprintf_s(CSName, "MsList - %x", ThreadID);
	mCS_Message = CreateEvent(NULL, FALSE, TRUE, (LPCWSTR)CSName);

}
int main()
{

}

  

原文地址:https://www.cnblogs.com/xiangshancuizhu/p/2711844.html