⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wrproc.cpp

📁 读者写者有限的问题
💻 CPP
字号:
#include"StdAfx.h"
#include "RWer.h"
#include"RWerDlg.h"
#include"WRproc.h"
//读写函数
UINT ReadProc(LPVOID lparam)
{
	CRWerDlg* hwnd=((PARAM*)lparam)->hwnd;
	TEST* infor=((PARAM*)lparam)->infor;//参数初始化	
	hwnd->addListBox(1,infor->index);//输出“线程创建”到列表框中		
	Sleep((infor->TStart)*1000);//延时	
	hwnd->addListBox(2,infor->index);//输出“线程申请资源”到列表框中

	hwnd->m_pMaxNumSemaphore->Lock();
	if(!hwnd->m_write_priority)//写者优先时执行
		hwnd->S.Lock();//*****若有写者在等待便会阻塞*********************************************8
	hwnd->RcountMutex.Lock();
	if(hwnd->Rcount==0)
		hwnd->writeMutex.Lock();
	hwnd->Rcount++;hwnd->Rwait--;
	hwnd->PostMessage(WM_READ_START,NULL,NULL);//消息
	hwnd->PostMessage(WM_R_WAIT,NULL,NULL);//消息
	hwnd->RcountMutex.Unlock();
	if(!hwnd->m_write_priority)//写者优先时执行
		hwnd->S.Unlock();//*********************************************8***
	hwnd->addListBox(3,infor->index);//输出“线程申请到资源”到列表框中	
	Sleep((infor->TContinu)*1000);//延时	
	hwnd->addListBox(4,infor->index);//输出“线程释放资源”到列表框中
	hwnd->RcountMutex.Lock();
	hwnd->Rcount--;hwnd->Rtemp++;
	hwnd->PostMessage(WM_READ_START,NULL,NULL);//消息
	hwnd->PostMessage(WM_READ_END,NULL,NULL);//消息
	if((++hwnd->finish)==hwnd->totalThread)
		hwnd->finished();//报告:"所有线程都已运行完毕!"
	if(hwnd->Rcount==0)	
		hwnd->writeMutex.Unlock();
	hwnd->RcountMutex.Unlock();		
	hwnd->m_pMaxNumSemaphore->Unlock();
	return 0;
}
UINT WriteProc(LPVOID lparam)
{	
	CRWerDlg* hwnd=((PARAM*)lparam)->hwnd;
	TEST* infor=((PARAM*)lparam)->infor;	
	hwnd->addListBox(5,infor->index);//输出“线程创建”到列表框中	
	Sleep((infor->TStart)*1000);//延时	
	hwnd->addListBox(6,infor->index);//输出“线程申请资源”到列表框中
	if(!hwnd->m_write_priority)//写者优先时执行
	{
		hwnd->WcountMutex.Lock();	
		if(hwnd->s==0)hwnd->S.Lock();
		hwnd->s++;		
		hwnd->WcountMutex.Unlock();	
	}//hwnd->PostMessage(WM_W_WAIT,NULL,NULL);//消息	
	hwnd->writeMutex.Lock();//*******************************************
	hwnd->addListBox(7,infor->index);//输出“线程申请到资源”到列表框中
	hwnd->Wwait--;hwnd->PostMessage(WM_W_WAIT,NULL,NULL);//消息	
	hwnd->Wcount=1;	hwnd->PostMessage(WM_WRITE_START,NULL,NULL);//消息
	Sleep((infor->TContinu)*1000);//延时	
	hwnd->addListBox(8,infor->index);//输出“线程释放资源”到列表框中	
	hwnd->Wcount=0;	hwnd->PostMessage(WM_WRITE_START,NULL,NULL);//消息
	hwnd->Wtemp++;hwnd->PostMessage(WM_WRITE_END,NULL,NULL);//消息		
	if((++hwnd->finish)==hwnd->totalThread)
		hwnd->finished();//报告:"所有线程都已运行完毕!"
	hwnd->writeMutex.Unlock();//*******************************************
	if(!hwnd->m_write_priority)//写者优先时执行
	{
		hwnd->WcountMutex.Lock();
		if(hwnd->s==0)hwnd->S.Unlock();
		hwnd->s--;		
		hwnd->WcountMutex.Unlock();
	}
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -