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

📄 semaphoreview.cpp

📁 Visual c++.程序设计培训教程
💻 CPP
字号:
// SemaphoreView.cpp : implementation of the CSemaphoreView class
//

#include "stdafx.h"
#include "Semaphore.h"

#include "SemaphoreDoc.h"
#include "SemaphoreView.h"
#include "afxmt.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

CSemaphore semaphore(2, 2, "semaphore1") ;  // 声明一个名为semaphore1的信号量

/////////////////////////////////////////////////////////////////////////////
// CSemaphoreView

IMPLEMENT_DYNCREATE(CSemaphoreView, CView)

BEGIN_MESSAGE_MAP(CSemaphoreView, CView)
	//{{AFX_MSG_MAP(CSemaphoreView)
	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()

/////////////////////////////////////////////////////////////////////////////
// CSemaphoreView construction/destruction

CSemaphoreView::CSemaphoreView()
{
	// TODO: add construction code here

}

CSemaphoreView::~CSemaphoreView()
{
}

BOOL CSemaphoreView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CSemaphoreView drawing

void CSemaphoreView::OnDraw(CDC* pDC)
{
	CSemaphoreDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CSemaphoreView printing

BOOL CSemaphoreView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CSemaphoreView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CSemaphoreView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CSemaphoreView diagnostics

#ifdef _DEBUG
void CSemaphoreView::AssertValid() const
{
	CView::AssertValid();
}

void CSemaphoreView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CSemaphoreDoc* CSemaphoreView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSemaphoreDoc)));
	return (CSemaphoreDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CSemaphoreView message handlers
/////////////////////////////////////////////////////////////////////////
//        下面为测试信号量的线程函数
UINT SemaphoreThread(LPVOID pParam)
{
    HWND hWnd = (HWND) pParam ;  // 转换线程函数参数为窗口句柄

    ::MessageBox(hWnd, "线程开始执行", // 弹出消息框告知用户线程开始执行
        "线程执行情况", MB_ICONEXCLAMATION | MB_OK);
    
	semaphore.Lock(); // 信号量可用资源减1,如果成功就弹出消息框告知用户
    ::MessageBox(hWnd, "线程锁定信号量",
        "线程执行情况", MB_ICONEXCLAMATION | MB_OK);
    
	semaphore.Unlock();// 信号量可用资源减1,并弹出消息框告知用户
    ::MessageBox(hWnd, "线程释放信号量",
        "线程执行情况", MB_ICONEXCLAMATION | MB_OK);
 
	return 0;
}

void CSemaphoreView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	HWND hWnd = GetSafeHwnd();     // 获取视图窗口的句柄
    AfxBeginThread(SemaphoreThread, hWnd);  // 产生新的线程
	
	CView::OnLButtonDown(nFlags, point);
}

⌨️ 快捷键说明

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