www.pudn.com > MutexDlg.rar > MutexDlg.cpp


// MutexDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "Mutex.h" 
#include "MutexDlg.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
void Randomize (); 
int Random (int num); 
 
///////////////////////////////////////////////////////////////////////////// 
// CMutexDlg dialog 
 
CMutexDlg::CMutexDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CMutexDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CMutexDlg) 
		// NOTE: the ClassWizard will add member initialization here 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CMutexDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CMutexDlg) 
	DDX_Control(pDX, IDC_MESSAGE_PROCESSOR, m_strProcessor); 
	DDX_Control(pDX, IDC_MESSAGE_RECEIVER, m_strReceiver); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CMutexDlg, CDialog) 
	//{{AFX_MSG_MAP(CMutexDlg) 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CMutexDlg message handlers 
 
BOOL CMutexDlg::OnInitDialog() 
{ 
	CDialog::OnInitDialog(); 
 
	// Set the icon for this dialog.  The framework does this automatically 
	//	when the application's main window is not a dialog 
	SetIcon(m_hIcon, TRUE); 		// Set big icon 
	SetIcon(m_hIcon, FALSE);		// Set small icon 
 
	Randomize (); 
	m_bmpKey.LoadBitmap (IDB_MUTEXKEY); 
	m_Images.Create (IDB_MUTEXKEY, 32, 2, ILC_MASK); 
 
	m_strReceiver.GetWindowRect (m_rcReceiver); 
	m_strProcessor.GetWindowRect (m_rcProcessor); 
	ScreenToClient (m_rcReceiver); 
	ScreenToClient (m_rcProcessor); 
	m_rcReceiver.right = m_rcReceiver.left - 10; 
	m_rcReceiver.left = m_rcReceiver.right - 32; 
	m_rcProcessor.left = m_rcReceiver.left; 
	m_rcProcessor.right = m_rcReceiver.right; 
// 
//	For a dialog box, ScreenToClient includes the title 
//	bar, so adjust for it. 
// 
	m_rcProcessor.top += 16; 
	m_rcProcessor.bottom += 16; 
	m_rcReceiver.top += 16; 
	m_rcReceiver.bottom += 16; 
 
	AfxBeginThread (StartReceiver, this); 
	AfxBeginThread (StartProcessor, this); 
 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
// If you add a minimize button to your dialog, you will need the code below 
//	to draw the icon.  For MFC applications using the document/view model, 
//	this is automatically done for you by the framework. 
 
void CMutexDlg::OnPaint()  
{ 
	if (IsIconic()) 
	{ 
		CPaintDC dc(this); // device context for painting 
 
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); 
 
		// Center icon in client rectangle 
		int cxIcon = GetSystemMetrics(SM_CXICON); 
		int cyIcon = GetSystemMetrics(SM_CYICON); 
		CRect rect; 
		GetClientRect(&rect); 
		int x = (rect.Width() - cxIcon + 1) / 2; 
		int y = (rect.Height() - cyIcon + 1) / 2; 
 
		// Draw the icon 
		dc.DrawIcon(x, y, m_hIcon); 
	} 
	else 
	{ 
		CDialog::OnPaint(); 
	} 
} 
 
// The system calls this to obtain the cursor to display while the user drags 
//	the minimized window. 
HCURSOR CMutexDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
void CMutexDlg::AddItemToQueue(QUEUEITEM *pItem) 
{ 
	m_strReceiver.SetWindowText ("Waiting for queue"); 
	CSingleLock AddQueue (&m_Mutex); 
	AddQueue.Lock (); 
	DrawKey (m_rcReceiver, true); 
	m_strReceiver.SetWindowText ("Queue is locked"); 
	Sleep (Random(1500)); 
	DrawKey (m_rcReceiver, false); 
	m_strReceiver.SetWindowText ("Queue is unlocked"); 
} 
 
void CMutexDlg::DeleteItemFromQueue(QUEUEITEM *pItem) 
{ 
	m_strProcessor.SetWindowText ("Waiting for queue"); 
	CSingleLock DeleteQueue (&m_Mutex); 
	DeleteQueue.Lock (); 
	DrawKey (m_rcProcessor, true); 
	m_strProcessor.SetWindowText ("Queue is locked"); 
	Sleep (Random(1500)); 
	DrawKey (m_rcProcessor, false); 
	m_strProcessor.SetWindowText ("Queue is unlocked"); 
} 
 
UINT CMutexDlg::StartReceiver(void *pParam) 
{ 
	CMutexDlg *pOwner = (CMutexDlg *) pParam; 
	QUEUEITEM qi; 
	while (1) 
	{ 
		pOwner->m_strReceiver.SetWindowText ("Receiving item"); 
		Sleep (Random (1000)); 
		pOwner->AddItemToQueue (&qi); 
	} 
	return (0); 
} 
 
UINT CMutexDlg::StartProcessor(void *pParam) 
{ 
	CMutexDlg *pOwner = (CMutexDlg *) pParam; 
	QUEUEITEM qi; 
	while (1) 
	{ 
		pOwner->m_strProcessor.SetWindowText ("Processing item"); 
		Sleep (Random (500)); 
		pOwner->DeleteItemFromQueue (&qi); 
	} 
	return (0); 
} 
 
void CMutexDlg::DrawKey(CRect &rc, bool bDraw) 
{ 
 
	CWindowDC dc(this); 
	if (bDraw == false) 
		m_Images.Draw (&dc, 1, CPoint (rc.left, rc.top), ILC_MASK); 
	else 
		m_Images.Draw (&dc, 0, CPoint (rc.left, rc.top), ILC_MASK); 
}