📄 mutexview.cpp
字号:
// MutexView.cpp : implementation of the CMutexView class
//
#include "stdafx.h"
#include "Mutex.h"
#include "MutexDoc.h"
#include "MutexView.h"
#include "afxmt.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CMutex mutex(FALSE,"mutex1"); // 声明一个名为mutex1的互斥量对象
/////////////////////////////////////////////////////////////////////////////
// CMutexView
IMPLEMENT_DYNCREATE(CMutexView, CView)
BEGIN_MESSAGE_MAP(CMutexView, CView)
//{{AFX_MSG_MAP(CMutexView)
ON_WM_LBUTTONDOWN()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMutexView construction/destruction
CMutexView::CMutexView()
{
// TODO: add construction code here
}
CMutexView::~CMutexView()
{
}
BOOL CMutexView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMutexView drawing
void CMutexView::OnDraw(CDC* pDC)
{
CMutexDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CMutexView printing
BOOL CMutexView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMutexView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMutexView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMutexView diagnostics
#ifdef _DEBUG
void CMutexView::AssertValid() const
{
CView::AssertValid();
}
void CMutexView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMutexDoc* CMutexView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMutexDoc)));
return (CMutexDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMutexView message handlers
/////////////////////////////////////////////////////////////////////////
// 下面为测试互斥量的线程函数
UINT MutexThread(LPVOID pParam)
{
HWND hWnd = (HWND) pParam; // 转换线程函数参数为窗口句柄
::MessageBox(hWnd, "线程开始执行。", // 弹出消息框告知用户线程开始执行
"线程执行情况", MB_ICONEXCLAMATION | MB_OK);
mutex.Lock(); // 锁定互斥量,如果成功就弹出消息框告知用户
::MessageBox(hWnd, "线程锁定互斥量。",
"线程执行情况", MB_ICONEXCLAMATION | MB_OK);
mutex.Unlock(); // 释放互斥量,并弹出消息框告知用户
::MessageBox(hWnd, "线程释放互斥量。",
"线程执行情况", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
void CMutexView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
HWND hWnd = GetSafeHwnd(); // 获取视图窗口的句柄
AfxBeginThread (MutexThread, hWnd); // 产生新的线程
CView::OnLButtonDown(nFlags, point);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -