www.pudn.com > wr.rar > wr.cpp
// wr.cpp : Defines the class behaviors for the application.
//
#include "stdafx.h"
#include "wr.h"
#include "wrDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CWrApp
int rcount=0; //读者数目
int wcount=0; //写者数目
CString str;
HANDLE rMutex=CreateMutex(NULL,FALSE,NULL);
HANDLE wMutex=CreateMutex(NULL,FALSE,NULL);
//CCriticalSection critical_section;
BEGIN_MESSAGE_MAP(CWrApp, CWinApp)
//{{AFX_MSG_MAP(CWrApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG
ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CWrApp construction
CWrApp::CWrApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CWrApp object
CWrApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CWrApp initialization
BOOL CWrApp::InitInstance()
{
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
CWrDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
DWORD WINAPI R1(LPVOID lpParameter)
{
CEdit *pEdit=(CEdit*)lpParameter;
if(wcount>0) AfxMessageBox("Somebody else is writing,you can't write");
else if(rcount==0)
{
WaitForSingleObject(wMutex,INFINITE);
WaitForSingleObject(rMutex,INFINITE);
}
else WaitForSingleObject(rMutex,INFINITE);
rcount++;
str="读者1正在读";
pEdit->SetWindowText(str);
Sleep(2000);
rcount--;
ReleaseMutex(rMutex);
if(rcount==0) ReleaseMutex(wMutex);
return 0;
}
DWORD WINAPI R2(LPVOID lpParameter)
{
CEdit *pEdit=(CEdit*)lpParameter;
if(wcount>0) AfxMessageBox("Somebody else is writing,you can't write");
else if(rcount==0)
{
WaitForSingleObject(wMutex,INFINITE);
WaitForSingleObject(rMutex,INFINITE);
}
else WaitForSingleObject(rMutex,INFINITE);
rcount++;
str="读者2正在读";
pEdit->SetWindowText(str);
Sleep(2000);
rcount--;
ReleaseMutex(rMutex);
if(rcount==0) ReleaseMutex(wMutex);
return 0;
}
DWORD WINAPI R3(LPVOID lpParameter)
{
CEdit *pEdit=(CEdit*)lpParameter;
if(wcount>0) AfxMessageBox("Somebody else is writing,you can't write");
else if(rcount==0)
{
WaitForSingleObject(wMutex,INFINITE);
WaitForSingleObject(rMutex,INFINITE);
}
else WaitForSingleObject(rMutex,INFINITE);
rcount++;
str="读者3正在读";
pEdit->SetWindowText(str);
Sleep(2000);
rcount--;
ReleaseMutex(rMutex);
if(rcount==0) ReleaseMutex(wMutex);
return 0;
}
DWORD WINAPI R4(LPVOID lpParameter)
{
CEdit *pEdit=(CEdit*)lpParameter;
if(wcount>0) AfxMessageBox("Somebody else is writing,you can't write");
else if(rcount==0)
{
WaitForSingleObject(wMutex,INFINITE);
WaitForSingleObject(rMutex,INFINITE);
}
else WaitForSingleObject(rMutex,INFINITE);
rcount++;
str="读者4正在读";
pEdit->SetWindowText(str);
Sleep(2000);
rcount--;
ReleaseMutex(rMutex);
if(rcount==0) ReleaseMutex(wMutex);
return 0;
}
DWORD WINAPI R5(LPVOID lpParameter)
{
CEdit *pEdit=(CEdit*)lpParameter;
if(wcount>0) AfxMessageBox("Somebody else is writing,you can't write");
else if(rcount==0)
{
WaitForSingleObject(wMutex,INFINITE);
WaitForSingleObject(rMutex,INFINITE);
}
else WaitForSingleObject(rMutex,INFINITE);
rcount++;
str="读者5正在读";
pEdit->SetWindowText(str);
Sleep(2000);
rcount--;
ReleaseMutex(rMutex);
if(rcount==0) ReleaseMutex(wMutex);
return 0;
}
DWORD WINAPI w1(LPVOID lpParameter)
{
CEdit *pEdit=(CEdit*)lpParameter;
if(rcount>0) AfxMessageBox("Somebody else is reading,you can't write");
else if(wcount==0)
{
WaitForSingleObject(wMutex,INFINITE);
wcount++;
str="我是写者1";
pEdit->SetWindowText(str);
ReleaseMutex(wMutex);
Sleep(2000);
wcount--;
}
else AfxMessageBox("Somebody else is writing,you can't write");
return 0;
}
DWORD WINAPI w2(LPVOID lpParameter)
{
CEdit *pEdit=(CEdit*)lpParameter;
if(rcount>0) AfxMessageBox("Somebody else is reading,you can't write");
else if(wcount==0)
{
WaitForSingleObject(wMutex,INFINITE);
wcount++;
str="我是写者2";
pEdit->SetWindowText(str);
ReleaseMutex(wMutex);
Sleep(2000);
wcount--;
}
else AfxMessageBox("Somebody else is writing,you can't write");
return 0;
}
DWORD WINAPI w3(LPVOID lpParameter)
{
CEdit *pEdit=(CEdit*)lpParameter;
if(rcount>0) AfxMessageBox("Somebody else is reading,you can't write");
else if(wcount==0)
{
WaitForSingleObject(wMutex,INFINITE);
wcount++;
str="我是写者3";
pEdit->SetWindowText(str);
ReleaseMutex(wMutex);
Sleep(2000);
wcount--;
}
else AfxMessageBox("Somebody else is writing,you can't write");
return 0;
}
DWORD WINAPI w4(LPVOID lpParameter)
{
CEdit *pEdit=(CEdit*)lpParameter;
if(rcount>0) AfxMessageBox("Somebody else is reading,you can't write");
else if(wcount==0)
{
WaitForSingleObject(wMutex,INFINITE);
wcount++;
str="我是写者4";
pEdit->SetWindowText(str);
ReleaseMutex(wMutex);
Sleep(2000);
wcount--;
}
else AfxMessageBox("Somebody else is writing,you can't write");
return 0;
}
DWORD WINAPI w5(LPVOID lpParameter)
{
CEdit *pEdit=(CEdit*)lpParameter;
if(rcount>0) AfxMessageBox("Somebody else is reading,you can't write");
else if(wcount==0)
{
WaitForSingleObject(wMutex,INFINITE);
wcount++;
str="我是写者5";
pEdit->SetWindowText(str);
ReleaseMutex(wMutex);
Sleep(2000);
wcount--;
}
else AfxMessageBox("Somebody else is writing,you can't write");
return 0;
}