?? wrproc.cpp
字號:
#include"StdAfx.h"
#include "RWer.h"
#include"RWerDlg.h"
#include"WRproc.h"
//讀寫函數(shù)
UINT ReadProc(LPVOID lparam)
{
CRWerDlg* hwnd=((PARAM*)lparam)->hwnd;
TEST* infor=((PARAM*)lparam)->infor;//參數(shù)初始化
hwnd->addListBox(1,infor->index);//輸出“線程創(chuàng)建”到列表框中
Sleep((infor->TStart)*1000);//延時
hwnd->addListBox(2,infor->index);//輸出“線程申請資源”到列表框中
hwnd->m_pMaxNumSemaphore->Lock();
if(!hwnd->m_write_priority)//寫者優(yōu)先時執(zhí)行
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)//寫者優(yōu)先時執(zhí)行
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);//輸出“線程創(chuàng)建”到列表框中
Sleep((infor->TStart)*1000);//延時
hwnd->addListBox(6,infor->index);//輸出“線程申請資源”到列表框中
if(!hwnd->m_write_priority)//寫者優(yōu)先時執(zhí)行
{
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)//寫者優(yōu)先時執(zhí)行
{
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 + -