c_cpp “Linux的环境高级编程”作业3
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp “Linux的环境高级编程”作业3相关的知识,希望对你有一定的参考价值。
#include <iostream>
#include <cstdlib>
using namespace std;
class CLQueueByList{
private:
struct QNode{
void* data;
struct QNode *next;
};
QNode* first; //队头指针
QNode* last; //队尾指针
public:
CLQueueByList(); //创建一个空队列
~CLQueueByList(); //销毁队列
bool push(void* e); //插入元素为e的新的队尾元素
bool pop(); //若队列不为空,则删除Q的队头元素
bool empty(); //队列是否为空
int size(); //返回队列的长度
void* front(); //返回队头元素
};
#include "CLQueueByList.h"
CLQueueByList::CLQueueByList() {
first=last=new QNode;
first->next=NULL;
}
CLQueueByList::~CLQueueByList() {
while(first){
last=first->next;
delete first;
first=last;
}
}
bool CLQueueByList::push(void* data) {
QNode *p=new QNode;
p->data=data;
p->next=NULL;
last->next=p; //尾指针指向插入的元素
last=p; //插入的元素成为尾指针
return true;
}
bool CLQueueByList::pop() {
if(first==last)
return false;
QNode *p=first->next;
first->next=p->next;
if(last==p)
last=first;
delete p;
return true;
}
bool CLQueueByList::empty() {
return first==last;
}
int CLQueueByList::size() {
int count=0;
if(first==last)
return 0;
QNode *p;
p=first->next;
while(p)
{
++count;
p=p->next;
}
return count;
}
void* CLQueueByList::front(){
if(first==last)
return NULL;
return first->next->data;
}
#ifndef CLMessageQueueBySTLqueue_H
#define CLMessageQueueBySTLqueue_H
#include "CLQueueByList.h"
#include "CLStatus.h"
#include "CLMutex.h"
#include "CLEvent.h"
class CLMessage;
class CLMessageQueueBySTLqueue
{
public:
CLMessageQueueBySTLqueue();
virtual ~CLMessageQueueBySTLqueue();
public:
CLStatus PushMessage(CLMessage * pMessage);
CLMessage* GetMessage();
private:
CLStatus Push(CLMessage * pMessage);
CLMessage* Pop();
private:
CLMessageQueueBySTLqueue(const CLMessageQueueBySTLqueue&);
CLMessageQueueBySTLqueue& operator=(const CLMessageQueueBySTLqueue&);
private:
CLQueueByList m_MessageQueue;
CLMutex m_Mutex;
CLEvent m_Event;
};
#endif
#include "CLMessageQueueBySTLqueue.h"
#include "CLCriticalSection.h"
#include "CLMessage.h"
#include "CLLogger.h"
CLMessageQueueBySTLqueue::CLMessageQueueBySTLqueue() : m_Event(true)
{
}
CLMessageQueueBySTLqueue::~CLMessageQueueBySTLqueue()
{
}
CLStatus CLMessageQueueBySTLqueue::PushMessage(CLMessage * pMessage)
{
if(pMessage == NULL)
return CLStatus(-1, 0);
CLStatus s = Push(pMessage);
if(!s.IsSuccess())
{
CLLogger::WriteLogMsg("In CLMessageQueue::PushMessage(), Push error", 0);
return CLStatus(-1, 0);
}
CLStatus s2 = m_Event.Set();
if(!s2.IsSuccess())
{
CLLogger::WriteLogMsg("In CLMessageQueue::PushMessage(), m_Event.Set error", 0);
return CLStatus(-1, 0);
}
return CLStatus(0, 0);
}
CLMessage* CLMessageQueueBySTLqueue::GetMessage()
{
CLStatus s = m_Event.Wait();
if(!s.IsSuccess())
{
CLLogger::WriteLogMsg("In CLMessageQueue::GetMessage(), m_Event.Wait error", 0);
return 0;
}
return Pop();
}
CLStatus CLMessageQueueBySTLqueue::Push(CLMessage * pMessage)
{
try
{
CLCriticalSection cs(&m_Mutex);
m_MessageQueue.push(pMessage);
}
catch(const char* str)
{
CLLogger::WriteLogMsg("In CLMessageQueue::Push(), exception arise", 0);
return CLStatus(-1, 0);
}
return CLStatus(0, 0);
}
CLMessage* CLMessageQueueBySTLqueue::Pop()
{
try
{
CLCriticalSection cs(&m_Mutex);
if(m_MessageQueue.empty())
return 0;
CLMessage *p = (CLMessage *) m_MessageQueue.front();
m_MessageQueue.pop();
return p;
}
catch(const char* str)
{
CLLogger::WriteLogMsg("In CLMessageQueue::Pop(), exception arise", 0);
return 0;
}
}
以上是关于c_cpp “Linux的环境高级编程”作业3的主要内容,如果未能解决你的问题,请参考以下文章
c_cpp “Linux的环境高级编程”作业1
c_cpp 【动态规划】流水作业调度【3.9】
c_cpp 【分支限界法】批处理作业调度
c_cpp 【回溯法】批处理作业调度【5.3】
c_cpp 第1周为C程序员编写的C ++作业
c_cpp 给定Ñ个作业的集合{J1,J2,......,JN}。每个作业必须先由机器1处理,然后由机器2处理。作业籍需要机器Ĵ的处理时间为TJI。对于一个确定的作业调度,设Fji是作业我在