#include <windows.h>
#include <process.h>
#include <iostream.h>
//using namespace std;
unsigned __stdcall reader1(PVOID pvParam);
unsigned __stdcall reader2(PVOID pvParam);
unsigned __stdcall writer1(PVOID pvParam);
unsigned __stdcall writer2(PVOID pvParam);
void write();
void read();
void realize_w();
void realized_r();
int Rcount=0;
int Wcount=0;
HANDLE Mut1;
HANDLE Mut2;
HANDLE Rmutex;
HANDLE Wmutex;
int main()
{
Mut1=::CreateSemaphore(NULL,1,1, NULL);
Mut2=::CreateSemaphore(NULL,1,1, NULL);
Rmutex=::CreateSemaphore(NULL,1,1, NULL);
Wmutex=::CreateSemaphore(NULL,1,1, NULL);
unsigned int threadid1;
unsigned int threadid2;
unsigned int threadid3;
unsigned int threadid4;
HANDLE hThread1;
HANDLE hThread2;
HANDLE hThread3;
HANDLE hThread4;
hThread1= (HANDLE)_beginthreadex(NULL,0,reader1,NULL,NULL,&threadid1);
hThread2= (HANDLE)_beginthreadex(NULL,0,reader2,NULL,NULL,&threadid2);
hThread3= (HANDLE)_beginthreadex(NULL,0,writer1,NULL,NULL,&threadid3);
hThread4= (HANDLE)_beginthreadex(NULL,0,writer2,NULL,NULL,&threadid4);
CloseHandle(hThread1);
CloseHandle(hThread2);
CloseHandle(hThread3);
CloseHandle(hThread4);
Sleep(20000);
return 0;
}
unsigned __stdcall reader1(PVOID pvParam)
{
while(true)
{
//WaitForSingleObject(Mut1,INFINITE);
//ReleaseSemaphore(Mut1,1,NULL);
WaitForSingleObject(Mut2,INFINITE);
Rcount=Rcount+1;
if(Rcount==1)
{ WaitForSingleObject(Rmutex,INFINITE); }
ReleaseSemaphore(Mut2,1,NULL);
cout<<"&&&&&&&&&&&&&&"<<endl;
//read();
cout<<"read A"<<endl;
cout<<"read B"<<endl;
cout<<"read C"<<endl;
cout<<"read D"<<endl;
WaitForSingleObject(Mut2,INFINITE);
Rcount=Rcount-1;
if(Rcount==0)
{ ReleaseSemaphore(Rmutex,1,NULL); }
ReleaseSemaphore(Mut2,1,NULL);
Sleep(1000);
}
return 0;
}
unsigned __stdcall reader2(PVOID pvParam)
{
while(true)
{
//WaitForSingleObject(Mut1,INFINITE);
//ReleaseSemaphore(Mut1,1,NULL);
WaitForSingleObject(Mut2,INFINITE);
Rcount=Rcount+1;
if(Rcount==1)
{ WaitForSingleObject(Rmutex,INFINITE); }
ReleaseSemaphore(Mut2,1,NULL);
cout<<"++++++++++++++++"<<endl;
//read();
cout<<"read E"<<endl;
cout<<"read F"<<endl;
cout<<"read G"<<endl;
cout<<"read H"<<endl;
WaitForSingleObject(Mut2,INFINITE);
Rcount=Rcount-1;
if(Rcount==0)
{ ReleaseSemaphore(Rmutex,1,NULL); }
ReleaseSemaphore(Mut2,1,NULL);
Sleep(1000);
}
return 0;
}
unsigned __stdcall writer1(PVOID pvParam)
{
while(true)
{
WaitForSingleObject(Mut1,INFINITE);
Wcount=Wcount+1;
if(Wcount==1)
{ WaitForSingleObject(Rmutex,INFINITE); }
ReleaseSemaphore(Mut1,1,NULL);
WaitForSingleObject(Wmutex,INFINITE);
//write();
cout<<"****************"<<endl;
cout<<"the first write A"<<endl;
cout<<"the second write B"<<endl;
cout<<"the third write C"<<endl;
cout<<"the forth write D"<<endl;
WaitForSingleObject(Mut1,INFINITE);
Wcount=Wcount-1;
if(Wcount==0)
{ ReleaseSemaphore(Rmutex,1,NULL); }
ReleaseSemaphore(Wmutex,1,NULL);
ReleaseSemaphore(Mut1,1,NULL);
Sleep(1000);
}
return 0;
}
unsigned __stdcall writer2(PVOID pvParam)
{
while(true)
{
WaitForSingleObject(Mut1,INFINITE);
Wcount=Wcount+1;
if(Wcount==1)
{ WaitForSingleObject(Rmutex,INFINITE); }
ReleaseSemaphore(Mut1,1,NULL);
WaitForSingleObject(Wmutex,INFINITE);
//write();
cout<<"-------------------"<<endl;
cout<<"the first write E"<<endl;
cout<<"the second write F"<<endl;
cout<<"the third write G"<<endl;
cout<<"the forth write H"<<endl;
WaitForSingleObject(Mut1,INFINITE);
Wcount=Wcount-1;
if(Wcount==0)
{ ReleaseSemaphore(Rmutex,1,NULL); }
ReleaseSemaphore(Wmutex,1,NULL);
ReleaseSemaphore(Mut1,1,NULL);
// use_data_write();
Sleep(1000);
}
return 0;
}
/*
void realized_r()
{
WaitForSingleObject(Mut1,INFINITE);
ReleaseSemaphore(Mut1);
WaitForSingleObject(Mut2,INFINITE);
Rcount=Rcount+1;
if(Rcount==1) WaitForSingleObject(Rmutex,INFINITE);
ReleaseSemaphore(Mut2);
cout<<"&&&&&&&&&&&&&&"<<endl;
//read();
WaitForSingleObject(Mut2,INFINITE);
Rcount=Rcount-1;
if(Rcount==0) ReleaseSemaphore(Rmutex);
ReleaseSemaphore(Mut2);
}
*/
/*void realize_w()
{
WaitForSingleObject(Mut1,INFINITE);
Wcount=Wcount+1;
if(Wcount==1)WaitForSingleObject(Rmutex,INFINITE);
ReleaseSemaphore(Mut1);
WaitForSingleObject(Wmutex,INFINITE);
//write();
cout<<"-------------------"<<endl;
WaitForSingleObject(Mut1,INFINITE);
Wcount=Wcount-1;
if(Wcount==0)ReleaseSemaphore(Rmutex);
ReleaseSemaphore(Wmutex);
ReleaseSemaphore(Mut1);
// use_data_write();
}
*/
void write()
{
cout<<"the first write A"<<endl;
cout<<"the second write B"<<endl;
cout<<"the third write C"<<endl;
cout<<"the forth write D"<<endl;
}
void read()
{
cout<<"read A"<<endl;
cout<<"read B"<<endl;
cout<<"read C"<<endl;
cout<<"read D"<<endl;
}
/*
typedef int semaphore;
semaphore Mut1=1;
semaphore Mut2=1;
semaphore Wmutex=1;
semaphore Rmutex=1;
int Rcount=0;
int Wcount=0;
void Writer()
{
down(Mut1);
Wcount=Wcount+1;
if(Wcount==1)down(Rmutex);
up(Mut1);
down(Wmutex);
write();
down(Mut1);
Wcount=Wcount-1;
if(Wcount==0)up(Rmutex);
up(Wmutex);
up(Mut1);
use_data_write();//非临界区
}
void Rreder()
{
down(Mut1);
up(Mut1);
down(Mut2);
Rcount=Rcount+1;
if(Rcount==1) down(Rmutex);
up(Mut2);
read();
down(Mut2);
Rcount=Rcount-1;
if(Rcount==0) up(Rmutex);
up(Mut2);
}
*/
没有合适的资源?快使用搜索试试~ 我知道了~
read—write问题c++实现
共13个文件
pdb:2个
dsw:1个
dsp:1个
4星 · 超过85%的资源 需积分: 17 11 下载量 32 浏览量
2010-05-21
23:12:38
上传
评论 1
收藏 383KB RAR 举报
温馨提示
read—write问题c++实现,程序比较简单,但是比较实用,希望对您有所帮助
资源推荐
资源详情
资源评论
收起资源包目录
read_write.rar (13个子文件)
read_write
PB2.plg 1KB
pb2.cpp 6KB
PB2.ncb 49KB
PB2.dsp 4KB
PB2.opt 53KB
Debug
vc60.pdb 140KB
vc60.idb 193KB
PB2.pch 183KB
PB2.exe 224KB
pb2.obj 26KB
PB2.ilk 345KB
PB2.pdb 1.02MB
PB2.dsw 529B
共 13 条
- 1
资源评论
- qazxcvbnm6182013-09-18还以为是读写文件的一些牛的操作,没想是这个东西,被坑了
alonefeng891023
- 粉丝: 0
- 资源: 22
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功