多线程-的-安全的队列.doc

多线程-的-安全的队列.doc

ID:59358068

大小:55.50 KB

页数:8页

时间:2020-09-04

多线程-的-安全的队列.doc_第1页
多线程-的-安全的队列.doc_第2页
多线程-的-安全的队列.doc_第3页
多线程-的-安全的队列.doc_第4页
多线程-的-安全的队列.doc_第5页
资源描述:

《多线程-的-安全的队列.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

1、#include#include#include#includeHANDLEhMutex;HANDLErMutex;//HANDLEpMutex;//////DWORDWINAPIFun1Proc(LPVOIDlpParameter);DWORDWINAPIFun2Proc(LPVOIDlpParameter);DWORDWINAPIFun3Proc(LPVOIDlpParameter);usingnamespacestd;intQueue_NUM=30;intj;constintNODENUM=50

2、0000;typedefintElemType;typedefstructqueueNode{ElemTypedata;structqueueNode*next;}QueueNode;typedefstruct{QueueNode*front;QueueNode*rear;}LinkQueue;LinkQueue*pQ;volatileintreadcount=0;//读者数目voidInitQueue(LinkQueue*HS){HS->front=newQueueNode;if(HS->front==NULL){printf("内存分配失败!");return;}HS->r

3、ear=HS->front;HS->front->next=NULL;}intIsEmpty(LinkQueue*HS){returnHS->front==HS->rear;}voidPrint(LinkQueue*l){Print函数没有用信号量保护QueueNode*p;p=l->front->next;while(p!=NULL){cout<data;p=p->next;}}voidEnQueue(LinkQueue*HS,ElemTypee){WaitForSingleObject(hMutex,-1);QueueNode*p;p=newQueueNode;if(p

4、==NULL){printf("内存分配失败!");return;此处返回前,没有释放mutex}p->data=e;p->next=NULL;HS->rear->next=p;HS->rear=p;Print(HS);ReleaseMutex(hMutex);}ElemTypeDeQueue(LinkQueue*HS){WaitForSingleObject(hMutex,-1);QueueNode*first;ElemTypetemp;if(IsEmpty(HS)){printf("");return0;此处返回前,没有释放mutex}Print(HS);first=H

5、S->front->next;temp=first->data;HS->front->next=first->next;if(HS->rear=first)HS->rear=HS->front;deletefirst;Print(HS);returntemp;此处返回,没有释放mutexReleaseMutex(hMutex);}QueueNode*Find(intdata,LinkQueue*l){WaitForSingleObject(rMutex,-1);if(readcount==0)WaitForSingleObject(hMutex,-1);readcount++;Re

6、leaseMutex(rMutex);//释放互斥信号QueueNode*p;p=l->front->next;WaitForSingleObject(rMutex,-1);代码位置不正确,而且还缺少一些readcount--;//读者数目减少while(p){if(p->data==data){returnp;}此处返回前,没有释放mutexp=p->next;}returnNULL;}voidClear(LinkQueue*HS)没有mutex保护{QueueNode*p,*q;p=HS->front->next;while(p!=NULL){q=p;p=p->next;del

7、eteq;}HS->front->next=NULL;HS->rear=HS->front;}voidDestroy(LinkQueue*HS){QueueNode*p,*q;p=HS->front;while(p!=NULL){q=p;p=p->next;deleteq;}HS->rear=HS->front=NULL;}DWORDWINAPIFun1Proc(LPVOIDlpParameter){for(j=0;j

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。