直接上类:
#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<<*pos< 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;
}