写完c语言代码怎样检查错误,哪个大神知道这个C语言代码的错误,成功运行有偿!...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

#include"stdafx.h"

#include"windows.h"

#include"iostream.h"

#include"math.h"

int long waiting(0);//等待理发的顾客人数

int chairs;//店中椅子的总数目

char open_door; //开门

charclose_door;//关门

int count(0);//顾客的序号

int finish(0);//已经理完发的顾客人数

DWORD a;

(无符号整数32位)

HANDLE Mutex=::CreateMutex(NULL,FALSE,"Mutex");//用来实现进程的互斥

HANDLE barbers=::CreateSemaphore(NULL,1,1,"barbers");//定义信号量来进行线程间的同步

HANDLE customers=::CreateSemaphore(NULL,0,3,"customers");//定义信号量来进行线程间的同步

int random()//

定义随机函数来产生顾客,并使两个顾客间的时间少于15秒 {

return(rand()*15000)/RAND_MAX;

}

void cuthair()//理发师的理发函数,用时15秒

{

::Sleep (15000);

cout<

}

void?gethaircut()?//?顾客被理发的函数

{

::Sleep?(15001);//顾客被理发的函数,为了和理发师之间有所区别,比理

发师理发时间长1毫秒

cout<

}

DWORD?WINAPI?customer(LPVOID?pParm2)

//顾客线程

{

::WaitForSingleObject(Mutex?,INFINITE);//P(mutex)来进行互斥操作

count++;//来的是第几个顾客

cout<

if(waiting

{

if(waiting!=0)

{

cout<

}

else

cout<

waiting++;

cout<

剩余"<

cout<

::ReleaseSemaphore(customers,1,NULL);//V(customer)

::ResumeThread(customers);//唤醒理发师进程

::ReleaseMutex(Mutex);//释放互斥量,以便其他线程使用

::WaitForSingleObject(barbers,INFINITE);//等待理发

gethaircut();//理发并离开

}

else

{

cout<

客直接离开

::ReleaseMutex(Mutex);

}

return 0;

}

DWORD?WINAPI?barber(LPVOID?pParm1)?//理发师线程

{

while(true)?//外循环

{

::WaitForSingleObject(customers,INFINITE);//p(customers),等待顾客

::WaitForSingleObject(Mutex,INFINITE);//等待互斥量

waiting--;//等待的人数减1

::ReleaseSemaphore(barbers,1,NULL);//释放信号量

::ResumeThread(barbers);//唤醒顾客进程

::ReleaseMutex(Mutex);

?//v(mutex);

cuthair();//理发

finish++;//理发完毕的顾客数目加1

}

return?0;

}

int?main(int?argc,?char*?argv[])//实现线程的操作

{

cout<

cin>>chairs;

cout<

cout<

cin>>open_door;

while?(open_door!='Y')

{

cout<

cout<

cin>>open_door;

}

HANDLE hThread1;

HADLE?hThread2;

hThread2=::CreateThread?(NULL,0,barber,NULL,0,NULL);//产生一个理发师进程

while(open_door='y') {

while(close_door!='y')

{

::Sleep(random());//函数实现顾客随机到来

hThread1=::CreateThread(NULL,0,customer,NULL,a,NULL);

cout<

if(finish>=8&&waiting==0)//如果完成数超过8并且没有人等待

{

cout<

cin>>close_door;

return close_door;

}

}

if (close_door=='y')

{

cout<

return?0;

}

return?0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值