欢迎来到天天文库
浏览记录
ID:58676186
大小:87.50 KB
页数:6页
时间:2020-10-15
《windows-并发的多线程的应用.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、(1)苹果香蕉问题#includeusingnamespacestd;#include#includeintk;HANDLEApple_;HANDLEBanana_;CRITICAL_SECTIONmmutex;DWORDWINAPISon(LPVOIDn){//HANDLEApple_;CRITICAL_SECTIONmmutex;inti=1;OpenSemaphore(MUTEX_ALL_ACCESS,false,"Apple_");while(1){::WaitForSing
2、leObject(Apple_,INFINITE);//等苹果cout<<"Soneats"<
3、SingleObject(Banana_,INFINITE);//等香蕉cout<<"Daughtereats"<
4、out<5、:CreateSemaphore(NULL,0,1,"banana");InitializeCriticalSection(&mmutex);srand(time(NULL));for(j=0;j<20;j++){Father_[j]=::CreateThread(NULL,0,Father,NULL,0,0);}::CreateThread(NULL,0,Son,NULL,0,0);::CreateThread(NULL,0,Daughter,NULL,0,0);Sleep(1000);WaitForMultipleObjects(20,6、Father_,TRUE,INFINITE);return0;}(2)苹果桔子问题#includeusingnamespacestd;#include#includeintk;HANDLEApple_;HANDLEOrange_;CRITICAL_SECTIONmmutex;DWORDWINAPISon(LPVOIDn){//HANDLEApple_;CRITICAL_SECTIONmmutex;inti=1;OpenSemaphore(MUTEX_ALL_ACCESS,false,7、"Orange_");while(1){::WaitForSingleObject(Orange_,INFINITE);//等桔子cout<<"Soneats"<8、,false,"Apple_");while(1){::WaitForSingleObject(Apple_,INFINITE);//等苹果cout<<"Daughtereats
5、:CreateSemaphore(NULL,0,1,"banana");InitializeCriticalSection(&mmutex);srand(time(NULL));for(j=0;j<20;j++){Father_[j]=::CreateThread(NULL,0,Father,NULL,0,0);}::CreateThread(NULL,0,Son,NULL,0,0);::CreateThread(NULL,0,Daughter,NULL,0,0);Sleep(1000);WaitForMultipleObjects(20,
6、Father_,TRUE,INFINITE);return0;}(2)苹果桔子问题#includeusingnamespacestd;#include#includeintk;HANDLEApple_;HANDLEOrange_;CRITICAL_SECTIONmmutex;DWORDWINAPISon(LPVOIDn){//HANDLEApple_;CRITICAL_SECTIONmmutex;inti=1;OpenSemaphore(MUTEX_ALL_ACCESS,false,
7、"Orange_");while(1){::WaitForSingleObject(Orange_,INFINITE);//等桔子cout<<"Soneats"<
8、,false,"Apple_");while(1){::WaitForSingleObject(Apple_,INFINITE);//等苹果cout<<"Daughtereats
此文档下载收益归作者所有