首页 > 代码库 > 读者写着问题
读者写着问题
读者优先
//读者写者问题-读者优先 #include<stdio.h> #include<process.h> #include<windows.h> using namespace std; HANDLE rmutex,wmutex; int Readcount=0; int read_num=10,write_num=5; DWORD WINAPI read(LPVOID lpParam) { WaitForSingleObject(rmutex,INFINITE); if(Readcount==0) { WaitForSingleObject(wmutex,INFINITE); printf("读者 %s-------------------------对wmutex已加锁\n",lpParam); } Readcount++; ReleaseMutex(rmutex); printf("读者 %s 开始读操作\n",lpParam); for(int i=0;i<5;i++) { Sleep(1); printf("读者 %s 正在读\n"); } printf("读者 %s 读操作完毕\n",lpParam); WaitForSingleObject(rmutex,INFINITE); Readcount--; if(Readcount==0) { ReleaseMutex(wmutex); printf("读者 %s-------------------------对wmutex已解锁\n",lpParam); } ReleaseMutex(rmutex); printf("读者 %s ******死亡\n",lpParam); //Sleep(10000); while(true) { Sleep(100); if(Readcount==0) { ReleaseMutex(wmutex); break; } } Sleep(10000); } DWORD WINAPI write(LPVOID lpParam) { WaitForSingleObject(wmutex,INFINITE); printf("写者 %s-------------------------对wmutex已加锁\n",lpParam); printf("写者 %s 开始写操作\n",lpParam); for(int i=0;i<5;i++) { Sleep(1); printf("写者 %s 正在写\n"); } printf("写者 %s 写操作完毕\n",lpParam); ReleaseMutex(wmutex); printf("写者 %s-------------------------对wmutext已解锁\n",lpParam); printf("写者 %s ******死亡\n"); Sleep(10000); } int main() { rmutex=CreateMutex(NULL,false,NULL); wmutex=CreateMutex(NULL,false,NULL); char *r[10]={"a","b","c","d","e","f","g","h","i","j"}; char *w[5]={"a","b","c","d","e"}; for(int i=0;i<read_num;i++) { printf("读者 %s 创建并申请读\n",r[i]); CreateThread(NULL,0,read,r[i],0,NULL); if(i>=5) { printf("写者 %s 创建并申请写\n",w[i-5]); CreateThread(NULL,0,write,w[i-5],0,NULL); } } Sleep(10000); return 0; }
写者优先
//读者写者问题-写者优先 #include<stdio.h> #include<windows.h> using namespace std; HANDLE rmutex,wmutex,rwmutex; int Readcount=0; int read_num=10,write_num=5; DWORD WINAPI read(LPVOID lpParam) { WaitForSingleObject(rwmutex,INFINITE); ReleaseMutex(rwmutex); WaitForSingleObject(rmutex,INFINITE); if(Readcount==0) { WaitForSingleObject(wmutex,INFINITE); printf("读者 %s-------------------------对wmutex已加锁\n",lpParam); } Readcount++; ReleaseMutex(rmutex); //read操作 printf("读者 %s 开始读操作\n",lpParam); for(int i=0; i<5; i++) { Sleep(1); printf("读者 %s 正在读\n"); } printf("读者 %s 读操作完毕\n",lpParam); WaitForSingleObject(rmutex,INFINITE); Readcount--; if(Readcount==0) { ReleaseMutex(wmutex); printf("读者 %s-------------------------对wmutex已解锁\n",lpParam); } ReleaseMutex(rmutex); printf("读者 %s ******死亡\n",lpParam); while(true) { Sleep(100); if(Readcount==0) { ReleaseMutex(wmutex); break; } } Sleep(10000); } DWORD WINAPI write(LPVOID lpParam) { WaitForSingleObject(rwmutex,INFINITE); WaitForSingleObject(wmutex,INFINITE); printf("写者 %s-------------------------对wmutex已加锁\n",lpParam); printf("写者 %s 开始写操作\n",lpParam); for(int i=0; i<5; i++) { Sleep(1); printf("写者 %s 正在写\n"); } printf("写者 %s 写操作完毕\n",lpParam); ReleaseMutex(wmutex); printf("写者 %s-------------------------对wmutext已解锁\n",lpParam); printf("写者 %s ******死亡\n"); ReleaseMutex(rwmutex); Sleep(10000); } int main() { rmutex=CreateMutex(NULL,false,NULL); wmutex=CreateMutex(NULL,false,NULL); rwmutex=CreateMutex(NULL,false,NULL); char *r[10]= {"a","b","c","d","e","f","g","h","i","j"}; char *w[5]= {"a","b","c","d","e"}; for(int i=0; i<read_num; i++) { printf("读者 %s 创建并申请读\n",r[i]); CreateThread(NULL,0,read,r[i],0,NULL); if(i>=5) { printf("写者 %s 创建并申请写\n",w[i-5]); CreateThread(NULL,0,write,w[i-5],0,NULL); } } Sleep(10000); return 0; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。