+ -
当前位置:首页 → 问答吧 → 有没有牛人可以把这程序改成线程安全型的队列。。

有没有牛人可以把这程序改成线程安全型的队列。。

时间:2011-12-07

来源:互联网

#include <iostream>
#include <fstream>
#include <iomanip>
#include <list>
#include <iterator>
#include <algorithm>
#include <cstring>
using namespace std;
#include <Windows.h>
#include <process.h>

typedef struct Node{
int iVal;
Node *pUp,*pDown;
}DNode,*PNode;

class NodeList{
public:
NodeList()
{
mHead=NULL;
mName[0]='\0';
mutex=CreateMutex(NULL,FALSE,NULL);
}
PNode Init();
bool ADD(PNode node);
void ADD(int val);
bool Delete(int val);
void Print();
PNode Find(int val,PNode pStart);
void Clean();
private:
PNode mHead;
char mName[1024];
HANDLE mutex;
};

bool NodeList::ADD(PNode node)

WaitForSingleObject(mutex,INFINITE);
bool bRet=false;
PNode tmp=mHead;
if(node){
if (tmp){
while(tmp->pDown){
tmp=tmp->pDown;
}
tmp->pDown=node;
node->pUp=tmp;
}else{
mHead=node;
}
bRet=true;

return bRet;
ReleaseMutex(mutex);
}

void NodeList::ADD(int val)
{
WaitForSingleObject(mutex,INFINITE);
PNode tmp=mHead , newNode; 
if(tmp)
while(tmp->pDown){
tmp=tmp->pDown;
}
newNode=new DNode();
newNode->iVal=val;
newNode->pDown=NULL;
if(tmp){
newNode->pUp=tmp;
tmp->pDown=newNode;
}else{
newNode->pUp=NULL;
mHead=newNode;

cout<<endl<<"Add Item is "<<val<<" success!"<<endl;
ReleaseMutex(mutex);
}

bool NodeList::Delete(int val)
{
WaitForSingleObject(mutex,INFINITE); 
PNode tmp=mHead;
bool bRet=false;
while(tmp){
if ( tmp->iVal==val){
bRet=true;
break;
}else{
tmp=tmp->pDown;
}
}
if(bRet){
if(tmp==mHead){
mHead=tmp->pDown;
if ( tmp->pDown ){
mHead->pUp=NULL;
}
}else{
tmp->pUp->pDown=tmp->pDown;
if ( tmp->pDown){
tmp->pDown->pUp=tmp->pUp;
}
}
delete(tmp);
cout<<endl<<mName<<"->操作:"<<" 删除值"<<val<<"成功!"<<endl;
}else{
cout<<endl<<mName<<"->操作:"<<" 删除值"<<val<<"不成功!"<<endl;

return bRet;
ReleaseMutex(mutex);
}

void NodeList::Print(){
  WaitForSingleObject(mutex,INFINITE);
PNode tmp=mHead;
int c=0;
cout<<endl<<mName<<"->操作:"<<" 显示 列表!"<<endl;
while(tmp){
cout<<tmp->iVal<<endl;
tmp=tmp->pDown;
c++;
}
cout<<endl<<mName<<"->操作:"<<" 显示 元素有"<<c<<"个!"<<endl;
ReleaseMutex(mutex);
}
PNode NodeList::Find(int val,PNode pStart)
{
  WaitForSingleObject(mutex,INFINITE);
PNode Ret=NULL;
PNode tmp=pStart;
while(tmp){
if ( tmp->iVal==val){
Ret=tmp;
break;
}else{
tmp=tmp->pDown;
}
}
return tmp;
ReleaseMutex(mutex);
}


void NodeList::Clean(){
WaitForSingleObject(mutex,INFINITE);
cout<<endl<<mName<<"->操作:"<<"清除 开始!"<<endl;
while(mHead){
if ( mHead->pDown ){
mHead=mHead->pDown;
delete(mHead->pUp);
}else{
delete(mHead);
mHead=NULL;
}
}
mHead=NULL;
cout<<endl<<mName<<"->操作:"<<"清除 结束!"<<endl;
  ReleaseMutex(mutex);
}


bool flag=true;
DWORD WINAPI subthread1(LPVOID);
DWORD WINAPI subthread2(LPVOID);
int main()
{
HANDLE hthread1;
HANDLE hthread2;
DWORD thread1;
DWORD thread2;
hthread1=CreateThread(NULL,0,subthread1,NULL,0,&thread1);
if(hthread1==NULL)
return -1;
hthread2=CreateThread(NULL,0,subthread2,NULL,0,&thread2);
if(hthread2==NULL)
return -1;
while(flag)
{
if(getchar())
{
flag=false;
}
}
getchar();
}
DWORD WINAPI subthread1(LPVOID lpParam)
{
while(flag)
{
NodeList tmp;
cout<<"线程1:";
tmp.ADD(1);
  Sleep(2000);
  tmp.ADD(3);
  Sleep(2000);
  tmp.ADD(5);
  Sleep(2000);
tmp.Print();
Sleep(2000);
tmp.Clean();
Sleep(3000);
}
return 0;
}

DWORD WINAPI subthread2(LPVOID lpParam)
{
while(flag)
{
NodeList tmp2;
cout<<"线程2:";
tmp2.ADD(2);
Sleep(2000);
  tmp2.ADD(4);
Sleep(2000);
  tmp2.ADD(6);
Sleep(2000);
tmp2.Delete(3);
Sleep(2000);
  tmp2.Print();
  Sleep(2000);
tmp2.Clean();
Sleep(3000);
}
return 0;
}
谁可以帮帮我,,把这线程安全型双向链表修改成线程安全性的队列。。
急求。。

作者: Rice1210   发布时间: 2011-12-07

链表改队列。。限制出入就行了吧?

作者: cloudzay   发布时间: 2011-12-07

引用 1 楼 cloudzay 的回复:

链表改队列。。限制出入就行了吧?
不懂。。完全对这方面不懂。。能不能把改好的结果发上来啊。。

作者: Rice1210   发布时间: 2011-12-07