用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c++代码库

模拟缓冲池

2012-10-13 作者: feng举报

[c++]代码库

<pre class="prettyprint brush:cpp;">#include <windows.h> #include <stdio.h> #include <iostream.h> #include <stdlib.h> #include <string> DWORD WINAPI Thread1(LPVOID lpPara); //线程1 DWORD WINAPI Thread2(LPVOID lpPara); //线程2 DWORD WINAPI Thread3(LPVOID lpPara); //线程3 HANDLE MS[3]; //互斥信号 HANDLE RS[3]; //同步信号 struct Buffer { int BufNo; //缓冲区号 int buf; //缓冲区内容 Buffer *next; //下一个缓冲区的指针 }; class BufferQueue //缓冲队列类 { public: BufferQueue(); ~BufferQueue(); bool isEmpty(); void enqueue(int n,int x=-1); //入队 Buffer *dequeue(); //出队 private: Buffer *front,*rear; }; BufferQueue::BufferQueue() //构造 { front=rear=NULL; } BufferQueue::~BufferQueue() //析构 { Buffer *q=front,*p; while(q!=NULL) { p=q; q=q->next; delete p; } front=rear=NULL; } bool BufferQueue::isEmpty() //判断是否空队列 { return front==NULL&&rear==NULL; } void BufferQueue::enqueue(int n,int x) //入队 { Buffer *q=new Buffer; if(x!=-1) //插入非空缓冲区 q->buf=x; q->BufNo=n; //缓冲区序号 if(isEmpty()) //空队列插入 front=q; else rear->next=q; //列尾插入 rear=q; } Buffer *BufferQueue::dequeue() //出队 { Buffer *q=front; front=front->next; if(front==NULL) //空队列时,首尾同步 rear=NULL; return q; } BufferQueue emp,inq,outq; //3个缓冲队列 Buffer* takeBuf(int type); //从type类型缓冲队列头取出一个缓冲区 void addBuf(int type,Buffer* buffer); //将buffer插入type类型队列尾 Buffer* getBuf(int type); //包含了同步和互斥控制的takeBuf操作 void putBuf(int type, Buffer* buffer); //包含了同步和互斥控制的addBuf操作 enum type{empty,input,output}; //类型 int main() { DWORD ThreadId1, ThreadId2,ThreadId3; for(int a=0;a<20;a++) //假设20个缓冲区 emp.enqueue(a); for(int a=0;a<3;a++) //3个互斥信号 MS[a]=CreateMutex(NULL,FALSE,"qemtx"); RS[0]=CreateSemaphore(NULL,20,20, "esp"); //3个同步信号 RS[1]=RS[2]=CreateSemaphore(NULL,0,20, "iosp"); HANDLE ithread = CreateThread(NULL,0,Thread1,NULL,0,&ThreadId1); HANDLE cthread = CreateThread(NULL,0,Thread2,NULL,0,&ThreadId1); HANDLE othread = CreateThread(NULL,0,Thread3,NULL,0,&ThreadId1); if(ithread == NULL||cthread == NULL||othread == NULL) return -1; getchar(); } Buffer* takeBuf(int type) //从type类型缓冲队列头取出一个缓冲区 { switch(type) { case 0: return emp.dequeue(); case 1: return inq.dequeue(); case 2: return outq.dequeue(); } } void addBuf(int type,Buffer* buffer) //将buffer插入type类型队列尾 { switch(type) { case 0: emp.enqueue(buffer->BufNo,buffer->buf); break; case 1: inq.enqueue(buffer->BufNo,buffer->buf); break; case 2: outq.enqueue(buffer->BufNo,buffer->buf); break; } } Buffer* getBuf(int type) //包含了同步和互斥控制的takeBuf操作 { WaitForSingleObject(RS[type],INFINITE); WaitForSingleObject(MS[type],INFINITE); Buffer *q= takeBuf(type); ReleaseMutex(MS[type]); return q; } void putBuf(int type, Buffer* buffer) //包含了同步和互斥控制的addBuf操作 { WaitForSingleObject(MS[type],INFINITE); addBuf(type,buffer); ReleaseSemaphore(RS[type],1,NULL); ReleaseMutex(MS[type]); } DWORD WINAPI Thread1(LPVOID lpPara) { int rn=1; while(rn<41) //假设40个整数 { Buffer *hin=getBuf(0); char s1[10],s2[10]; string tr=string("输入进程: 缓冲单元")+itoa(hin->BufNo,s1,10)+" 收容输入 data="+itoa(rn,s2,10)+"\n"; cout<<tr; hin->buf=rn; putBuf(1,hin); rn++; Sleep(120); } } DWORD WINAPI Thread2(LPVOID lpPara) { int n=0; while(true) { if(!inq.isEmpty()) { Buffer *sin=getBuf(1); n=sin->buf; sin->buf=-1; putBuf(0,sin); char s1[10],s2[10]; string tr=string("计算进程: 缓冲单元")+itoa(sin->BufNo,s1,10)+" 提取输入 data="+itoa(n,s2,10)+"\n"; cout<<tr; Sleep(80); n=n+10000; Buffer *hout=getBuf(0); tr=string("计算进程: 缓冲单元")+itoa(hout->BufNo,s1,10)+" 收容输出 data="+itoa(n,s2,10)+"\n"; cout<<tr; hout->buf=n; putBuf(2,hout); } } } DWORD WINAPI Thread3(LPVOID lpPara) { while(true) { if(!outq.isEmpty()) { Buffer *sout=getBuf(2); char s1[10],s2[10]; string tr=string("输出进程: 缓冲单元")+itoa(sout->BufNo,s1,10)+" 提取输出 data="+itoa(sout->buf,s2,10)+"\n"; cout<<tr; sout->buf=-1; putBuf(0,sout); Sleep(120); } } } </pre> <br /><p>   </p>


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...