?? reader_writer.cpp
字號:
#include<fstream.h>
#include<windows.h>
const char* test_file="thread.dat";
const int max=100;
long readcount=0,writecount=0;
//線程信息結構
struct ThreadInfo{
unsigned long id;
char RorW;
double createtime;
double runtime;
}thrdinfo[max];
//讀寫單元
void rwunit(ThreadInfo* p)
{
if(p->RorW=='R'){
cout<<"線程 "<<p->id<<" 開始讀操作."<<endl;
Sleep((DWORD)p->runtime*1000);
cout<<"線程 "<<p->id<<" 結束讀操作."<<endl;
}
else{
cout<<"線程 "<<p->id<<" 開始寫操作."<<endl;
Sleep((DWORD)p->runtime*1000);
cout<<"線程 "<<p->id<<" 結束寫操作."<<endl;
}
}
//讀優先寫操作
void RP_WriterThread(LPVOID lpParam)
{
ThreadInfo* p=(ThreadInfo*)lpParam;
Sleep((DWORD)p->createtime*1000);
cout<<"線程 "<<p->id<<" 申請寫操作."<<endl;
HANDLE wsem=OpenSemaphore(SEMAPHORE_ALL_ACCESS,false,"wsem");
WaitForSingleObject(wsem,INFINITE);
rwunit(p);
ReleaseSemaphore(wsem,1,NULL);
CloseHandle(wsem);
}
//讀優先讀操作
void RP_ReadThread(LPVOID lpParam)
{
ThreadInfo* p=(ThreadInfo*)lpParam;
Sleep((DWORD)p->createtime*1000);
cout<<"線程 "<<p->id<<" 申請讀操作."<<endl;
HANDLE x=OpenMutex(MUTEX_ALL_ACCESS,false,"x");
HANDLE wsem=OpenSemaphore(SEMAPHORE_ALL_ACCESS,false,"wsem");
WaitForSingleObject(x,INFINITE);
readcount++;
if(readcount==1)WaitForSingleObject(wsem,INFINITE);
ReleaseMutex(x);
rwunit(p);
WaitForSingleObject(x,INFINITE);
readcount--;
if(readcount==0)ReleaseSemaphore(wsem,1,NULL);
ReleaseMutex(x);
CloseHandle(x);
CloseHandle(wsem);
}
//寫優先寫控制
void WP_WriterThread(LPVOID lpParam)
{
ThreadInfo* p=(ThreadInfo*)lpParam;
Sleep((DWORD)p->createtime*1000);
cout<<"線程 "<<p->id<<" 申請寫操作."<<endl;
HANDLE y=OpenMutex(MUTEX_ALL_ACCESS,false,"y");
HANDLE wsem=OpenSemaphore(SEMAPHORE_ALL_ACCESS,false,"wsem");
HANDLE rsem=OpenSemaphore(SEMAPHORE_ALL_ACCESS,false,"rsem");
WaitForSingleObject(y,INFINITE);
writecount++;
if(writecount==1) WaitForSingleObject(rsem,INFINITE);
ReleaseMutex(y);
WaitForSingleObject(wsem,INFINITE);
rwunit(p);
ReleaseSemaphore(wsem,1,NULL);
WaitForSingleObject(y,INFINITE);
writecount--;
if(writecount==0) ReleaseSemaphore(rsem,1,NULL);
ReleaseMutex(y);
CloseHandle(y);
CloseHandle(rsem);
CloseHandle(wsem);
}
//寫優先讀操作
void WP_ReadThread(LPVOID lpParam)
{
ThreadInfo* p=(ThreadInfo*)lpParam;
Sleep((DWORD)p->createtime*1000);
cout<<"線程 "<<p->id<<" 申請讀操作."<<endl;
HANDLE z=OpenMutex(MUTEX_ALL_ACCESS,false,"z");
HANDLE x=OpenMutex(MUTEX_ALL_ACCESS,false,"x");
HANDLE wsem=OpenSemaphore(SEMAPHORE_ALL_ACCESS,false,"wsem");
HANDLE rsem=OpenSemaphore(SEMAPHORE_ALL_ACCESS,false,"rsem");
WaitForSingleObject(z,INFINITE);
WaitForSingleObject(rsem,INFINITE);
WaitForSingleObject(x,INFINITE);
readcount++;
if(readcount==1) WaitForSingleObject(wsem,INFINITE);
ReleaseMutex(x);
ReleaseSemaphore(rsem,1,NULL);
ReleaseMutex(z);
rwunit(p);
WaitForSingleObject(x,INFINITE);
readcount--;
if(readcount==0) ReleaseSemaphore(wsem,1,NULL);
ReleaseMutex(x);
CloseHandle(z);
CloseHandle(x);
CloseHandle(wsem);
CloseHandle(rsem);
}
int main()
{
long i=0,maxthrd;
char choice;
ifstream input_file;
//以只讀方式打開文件
input_file.open(test_file);
if(!input_file){
cout<<"Can not open file thread.dat!"<<endl;
}
//讀thread.dat
while(input_file>>thrdinfo[i].id>>thrdinfo[i].RorW>>thrdinfo[i].createtime>>thrdinfo[i].runtime)
i++;
input_file.close();
maxthrd=i;//最大線程數
cout<<"讀優先(R)/寫優先(W)?:";
cin>>choice;
CreateSemaphore(NULL,1,1,"wsem");
CreateSemaphore(NULL,1,1,"rsem");
DWORD* thread_id=new DWORD[maxthrd];
HANDLE* h_thread=new HANDLE[maxthrd];
//創建線程
if(choice=='R'||choice=='r'){ //創建讀優先
for(i=0;i<=maxthrd-1;i++){
if(thrdinfo[i].RorW=='W')
h_thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(RP_WriterThread),
&(thrdinfo[i]),0,&thread_id[i]);
else
h_thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(RP_ReadThread),
&(thrdinfo[i]),0,&thread_id[i]);
if(h_thread[i]==NULL) CloseHandle(h_thread[i]);
cout<<"線程 "<<thrdinfo[i].id<<" 已創建.("<<thrdinfo[i].RorW<<" "<<thrdinfo[i].createtime
<<" "<<thrdinfo[i].runtime<<")"<<endl;
}
}
else{ //創建寫優先
for(i=0;i<=maxthrd-1;i++){
if(thrdinfo[i].RorW=='W')
h_thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(WP_WriterThread),
&(thrdinfo[i]),0,&thread_id[i]);
else h_thread[i]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(WP_ReadThread),
&(thrdinfo[i]),0,&thread_id[i]);
if(h_thread[i]==NULL) CloseHandle(h_thread[i]);
cout<<"線程 "<<thrdinfo[i].id<<" 已創建.("<<thrdinfo[i].RorW<<" "<<thrdinfo[i].createtime
<<" "<<thrdinfo[i].runtime<<")"<<endl;
}
}
for(i=0;i<maxthrd;i++)
ResumeThread(h_thread[i]);
//結束所有線程
for(i=0;i<maxthrd;i++){
ExitThread(thread_id[i]);
CloseHandle(h_thread[i]);
}
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -