读者写者问题:读者优先,允许多个读者同时读,但只允许一个写者写
#include<windows.h>#include<iostream>usingnamespacestd;intreadercount=0;HANDLEr,w;CRI...
#include<windows.h>
#include<iostream>
using namespace std;
int readercount=0;
HANDLE r,w;
CRITICAL_SECTION cs;
DWORD WINAPI writer(PVOID pArg){
int ww=(int)pArg;
DWORD t;
while(true){
t=WaitForSingleObject(w,1);
if(t==WAIT_TIMEOUT)
{
EnterCriticalSection(&cs);
cout<<"writer "<<ww<<" waite to read...."<<endl;
LeaveCriticalSection(&cs);
WaitForSingleObject(w,INFINITE);
}
EnterCriticalSection(&cs);
cout<<"writer "<<ww<<" start to write...."<<endl;
LeaveCriticalSection(&cs);
Sleep(3000);
EnterCriticalSection(&cs);
cout<<"writer "<<ww<<" finish writing...."<<endl;
LeaveCriticalSection(&cs);
Sleep(5000);
ReleaseSemaphore(w, 1, NULL);
}
return 0;
}
DWORD WINAPI reader(PVOID pArg){
int rr=(int)pArg;
DWORD t;
while(true){
readercount++;
if(readercount==1)
{
t=WaitForSingleObject(w,1);
if(t==WAIT_TIMEOUT)
{
EnterCriticalSection(&cs);
cout<<" reader "<<rr<<" waite to read...."<<endl;
LeaveCriticalSection(&cs);
WaitForSingleObject(w,INFINITE);
}
}
EnterCriticalSection(&cs);
cout<<" reader "<<rr<<" start to read...."<<endl;
LeaveCriticalSection(&cs);
Sleep(3000);
EnterCriticalSection(&cs);
cout<<" reader "<<rr<<" finish reading...."<<endl;
LeaveCriticalSection(&cs);
Sleep(5000);
readercount--;
if(readercount==0)
ReleaseSemaphore(w, 1, NULL);
}
return 0;
}
int main(){
int i;
InitializeCriticalSection(&cs);
w = CreateSemaphore(NULL, 1, 1, NULL);
HANDLE Thread[6];
for(i=0;i<3;i++)
Thread[i]=CreateThread(NULL,0,reader,(LPVOID)(i+1),0,NULL);
for(i=3;i<5;i++)
Thread[i]=CreateThread(NULL,0,writer,(LPVOID)(i-2),0,NULL);
WaitForMultipleObjects(5, Thread, TRUE, INFINITE);
CloseHandle(r);
DeleteCriticalSection(&cs);
return 0;
} 展开
#include<iostream>
using namespace std;
int readercount=0;
HANDLE r,w;
CRITICAL_SECTION cs;
DWORD WINAPI writer(PVOID pArg){
int ww=(int)pArg;
DWORD t;
while(true){
t=WaitForSingleObject(w,1);
if(t==WAIT_TIMEOUT)
{
EnterCriticalSection(&cs);
cout<<"writer "<<ww<<" waite to read...."<<endl;
LeaveCriticalSection(&cs);
WaitForSingleObject(w,INFINITE);
}
EnterCriticalSection(&cs);
cout<<"writer "<<ww<<" start to write...."<<endl;
LeaveCriticalSection(&cs);
Sleep(3000);
EnterCriticalSection(&cs);
cout<<"writer "<<ww<<" finish writing...."<<endl;
LeaveCriticalSection(&cs);
Sleep(5000);
ReleaseSemaphore(w, 1, NULL);
}
return 0;
}
DWORD WINAPI reader(PVOID pArg){
int rr=(int)pArg;
DWORD t;
while(true){
readercount++;
if(readercount==1)
{
t=WaitForSingleObject(w,1);
if(t==WAIT_TIMEOUT)
{
EnterCriticalSection(&cs);
cout<<" reader "<<rr<<" waite to read...."<<endl;
LeaveCriticalSection(&cs);
WaitForSingleObject(w,INFINITE);
}
}
EnterCriticalSection(&cs);
cout<<" reader "<<rr<<" start to read...."<<endl;
LeaveCriticalSection(&cs);
Sleep(3000);
EnterCriticalSection(&cs);
cout<<" reader "<<rr<<" finish reading...."<<endl;
LeaveCriticalSection(&cs);
Sleep(5000);
readercount--;
if(readercount==0)
ReleaseSemaphore(w, 1, NULL);
}
return 0;
}
int main(){
int i;
InitializeCriticalSection(&cs);
w = CreateSemaphore(NULL, 1, 1, NULL);
HANDLE Thread[6];
for(i=0;i<3;i++)
Thread[i]=CreateThread(NULL,0,reader,(LPVOID)(i+1),0,NULL);
for(i=3;i<5;i++)
Thread[i]=CreateThread(NULL,0,writer,(LPVOID)(i-2),0,NULL);
WaitForMultipleObjects(5, Thread, TRUE, INFINITE);
CloseHandle(r);
DeleteCriticalSection(&cs);
return 0;
} 展开
3个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询