当前位置:   article > 正文

queue sample

samplequeue

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);
};

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/295811
推荐阅读
  

闽ICP备14008679号