taskqueue.cpp

来自「在linux下基于UDP通讯的程序,包括客户端与服务端.」· C++ 代码 · 共 68 行

CPP
68
字号
#include "TaskQueue.h"
#include "CommHead.h"
#include "iostream"
#include "LogHelper.h"

using namespace std;

CTaskQueue  theQueue;
LockType g_TaskQueueLock;
extern CLogHelper theLogger;
CTaskQueue::CTaskQueue()
{
  InitLock(&g_TaskQueueLock);	
}

CTaskQueue::~CTaskQueue()
{
	
}
//向任务队列中添加任务对象
bool CTaskQueue::PushTask(CTask *task)
{
	if(task != NULL)
	{  
		string str = "添加一个任务到队列中[CTaskQueue::PushTask(CTask *task)]";
		theLogger.LogMessage(str, true);
		LockOn(&g_TaskQueueLock);
		m_pTaskList.push(task);
		LockOff(&g_TaskQueueLock);
		return true;
	}
	else
	{
		return false;
	}
}

//从任务对列中取出来一个任务对象
//返回指向新的对象的指针
CTask* CTaskQueue::FetchTask()
{
	CTask * task = NULL;
	LockOn(&g_TaskQueueLock);
	if(!m_pTaskList.empty())
	{
		task = m_pTaskList.front();
		m_pTaskList.pop();
	}
	LockOff(&g_TaskQueueLock);
    return task;
}

//得到任务队列中任务对象的个数
inline int CTaskQueue::Size() const
{
	int nRet = 0;
	LockOn(&g_TaskQueueLock);
	nRet = m_pTaskList.size();
	LockOff(&g_TaskQueueLock);
	return nRet;
}
bool CTaskQueue::IsEmpty() const
{
	return (Size() == 0);
}


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?