잠 금 순환 대기 열 이 있 는 패키지 클래스

직접 클래스:
#ifndef _ZZC_DEQUE_
#define _ZZC_DEQUE_

#include "MutexLock.h"
#include 
#include 
using namespace std;

template 
class ZZCdeque
{
public:
	ZZCdeque(unsigned int maxlength = 5000)
	{
		m_nMaxlength = maxlength;
	}

	~ZZCdeque(void)
	{}

	/*
	      :              
		   :  -0,  -1
	*/
	int push(const _T& t)
	{
		int nRet = 0;

		m_lock.Lock();

		if (m_deque.size() < m_nMaxlength)
		{
			m_deque.push_front(t);
			nRet = 1;
		}

		m_lock.Unlock();

		return nRet;
	}

	/*
	      :                
		   :         
	*/
	int push(deque<_t>& indeque)
	{
		int nNum(0);

		int nIndeque = indeque.size();
		if(nIndeque <= 0)
			return 0;

		if(m_nMaxlength - m_deque.size() <= 0)
			return 0;

		m_lock.Lock();

		if (nIndeque <= m_nMaxlength - m_deque.size())
		{
			for (int i = 0;i < nIndeque;i++)
			{
				_T t = indeque.back();
				m_deque.push_front(t);
				indeque.pop_back();

				nNum++;
			}
		} 
		else
		{
			for (int i = 0;i < m_nMaxlength - m_deque.size();i++)
			{
				_T t = indeque.back();
				m_deque.push_front(t);
				indeque.pop_back();

				nNum++;
			}
		}

		m_lock.Unlock();

		return nNum;
	}

	/*
	      :              
		   :  -0,  -1
	*/
	int pop(_T& t)
	{
		int nRet = 0;

		m_lock.Lock();
		if (m_deque.size() > 0)
		{
			t = m_deque.back();
			m_deque.pop_back();
			nRet = 1;
		}
		m_lock.Unlock();

		return nRet;
	}

	/*
	      :                
		   :         
	*/
	int pop(deque<_t>& outdeque,int n)
	{
		int nNum(0);
		m_lock.Lock();

		for (int i = 0;i < n;i++)
		{
			if (m_deque.size() > 0)
			{
				_T t = m_deque.back();
				outdeque.push_front(t);
				m_deque.pop_back();

				int nn = m_deque.size();
				nNum++;
			}
			else
			{
				break;
			}
		}

		m_lock.Lock();

		return nNum;
	}

	int GetSize()
	{
		m_lock.Lock();

		return m_deque.size();

		m_lock.Unlock();
	}

	int GetAllSize()
	{
		return m_nMaxlength;
	}

	/*
	       
	*/
	void Traverse()
	{
		m_lock.Lock();

		deque<_t>::iterator pos;
		for (pos = m_deque.begin(); pos != m_deque.end(); pos++)
			cout< m_deque;
};

#endif

상호 배척 자물쇠 의 포장 은 다음 과 같다.
#ifndef _MUTEX_LOCK_
#define _MUTEX_LOCK_

#include "Windows.h"

class MutexHandleMgr
{
public:
	MutexHandleMgr():hMutex(NULL){}
public:
	HANDLE hMutex;
};

class MutexLock
{
public:
	MutexLock(void);
	~MutexLock(void);

	int Lock();
	int Unlock();
	int Trylock(DWORD dwMilliseconds = 1);
private:
	int Initlock();
	int Releaselock();

private:
	MutexHandleMgr* m_hMgr;
};

#endif


#include "StdAfx.h"
#include "MutexLock.h"


MutexLock::MutexLock(void):m_hMgr(NULL)
{
	Initlock();
}


MutexLock::~MutexLock(void)
{
	Releaselock();
}

/*
      :      
	   :  -0,  -1
*/
int MutexLock::Initlock()
{
	m_hMgr = new MutexHandleMgr;
    if (m_hMgr == NULL)
		return 0;

	m_hMgr->hMutex = CreateMutex(NULL, true, NULL);
	if (m_hMgr->hMutex == NULL)
	{
		delete m_hMgr;
		m_hMgr = NULL;
		return 0;
	}

	ReleaseMutex(m_hMgr->hMutex);

	return 1;
}

/*
      :     
	   :  -0,  -1
*/
int MutexLock::Releaselock()
{
	if(m_hMgr) 
	{
		CloseHandle(m_hMgr->hMutex);
		delete m_hMgr;
		m_hMgr = NULL;

		return 1;
	}
	else
	{
		return 0;
	}
}

/*
      :  
	   :  -0,  -1
*/
int MutexLock::Lock()
{
	if(!m_hMgr) return 0;

	WaitForSingleObject(m_hMgr->hMutex, INFINITE);

	return 1;
}

/*
      :  
	   :  -0,  -1
*/
int MutexLock::Unlock()
{
	if(!m_hMgr) return 0;

	ReleaseMutex(m_hMgr->hMutex);

	return 1;
}

/*
      :    
	   :  -0,  -1
*/
int MutexLock::Trylock(DWORD dwMilliseconds)
{
	if(!m_hMgr) return 0;

	DWORD  ret = WaitForSingleObject(m_hMgr->hMutex, dwMilliseconds);	
	if( ret == WAIT_OBJECT_0)
	{
		return 1; 
	}
	if( ret == WAIT_TIMEOUT)
	{
		return 0;
	}
	return 1;
}

좋은 웹페이지 즐겨찾기