赞
踩
http://www.iteye.com/topic/477412
#include "StdAfx.h"
#include "TaskQueue.h"
#include <process.h>
#include <boost/thread/shared_mutex.hpp>
#include <boost/thread.hpp>
using namespace std;
typedef boost::shared_mutex rwmutex;
typedef boost::shared_lock<rwmutex> readLock;
typedef boost::unique_lock<rwmutex> writeLock;
rwmutex _rwmutex;
static TaskQueue* taskQueueInstance;
TaskQueue::TaskQueue()
{
}
TaskQueue::~TaskQueue(void)
{
}
TaskQueue* TaskQueue::GetInstance()
{
if(taskQueueInstance == NULL)
{
taskQueueInstance = new TaskQueue();
}
return taskQueueInstance;
}
int TaskQueue::GetSize()
{
try
{
int size = TQ.size();
return size;
}
catch (const std::exception& ex)
{
fprintf(stderr, "reason: %s\n", ex.what());
return 0;
}
}
void TaskQueue::GetQueue(queue<taskInfo>& queueTaskInfo)
{
writeLock lk(_rwmutex);
try
{
queueTaskInfo = TQ;
lk.unlock();
}
catch (const std::exception& ex)
{
fprintf(stderr, "reason: %s\n", ex.what());
}
}
void TaskQueue::Push(taskInfo ti)
{
writeLock lk(_rwmutex);
try
{
TQ.push(ti);
lk.unlock();
}
catch (const std::exception& ex)
{
fprintf(stderr, "reason: %s\n", ex.what());
}
}
void TaskQueue::Front(taskInfo& ti)
{
if(TQ.size()>0)
{
writeLock lk(_rwmutex);
ti = TQ.front();
lk.unlock();
}
}
void TaskQueue::Back()
{
if(TQ.size()>0)
{
writeLock lk(_rwmutex);
TQ.back();
lk.unlock();
}
}
void TaskQueue::Pop()
{
if(TQ.size()>0)
{
writeLock lk(_rwmutex);
TQ.pop();
lk.unlock();
}
}
void TaskQueue::Empty()
{
writeLock lk(_rwmutex);
while(!TQ.empty())
{
TQ.pop();
}
lk.unlock();
}
#pragma once
#include "DataModel.h"
#include <queue>
class TaskQueue
{
public:
static TaskQueue* TaskQueue::GetInstance();
queue<taskInfo> TQ;
int TaskQueue::GetSize();
void TaskQueue::GetQueue(queue<taskInfo>& queueTaskInfo);
void TaskQueue::Push(taskInfo ti);
void TaskQueue::Front(taskInfo& ti);
void TaskQueue::Pop();
void TaskQueue::Back();
void TaskQueue::Empty();
public:
TaskQueue();
~TaskQueue(void);
};
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。