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

📄 producerthread.cppevent

📁 此文件用于实现生产者消费者的问题
💻 CPPEVENT
字号:
/************************************************************************
* 版权所有 (C)2003,windcao(曹建峰)
* 作    者: 曹建峰
* 完成日期: 2004年1月9日
* 修改日期    版本号     修改人	     修改内容
************************************************************************/
// ProducerThread.cpp : implementation file
//

#include "stdafx.h"
#include "P_C.h"
#include "ProducerThread.h"
#include "P_CDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CProducerThread

IMPLEMENT_DYNCREATE(CProducerThread, CWinThread)

CProducerThread::CProducerThread(void* hParent)
:m_pParentDlg(hParent)
{ 
	//	this->m_pParent
}

CProducerThread::CProducerThread()
{
}

CProducerThread::~CProducerThread()
{
}

BOOL CProducerThread::InitInstance()
{
	// TODO:  perform and per-thread initialization here
	return TRUE;
}

int CProducerThread::ExitInstance()
{
	// TODO:  perform any per-thread cleanup here
	return CWinThread::ExitInstance();
}

BEGIN_MESSAGE_MAP(CProducerThread, CWinThread)
	//{{AFX_MSG_MAP(CProducerThread)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CProducerThread message handlers

int CProducerThread::Run() 
{
	// TODO: Add your specialized code here and/or call the base class
	CP_CDlg *pDlg;
	pDlg=(CP_CDlg*)m_pParentDlg;

	CMutex *pMutex;
	CEvent *pFull;
	CEvent *pEmpty;
	pFull=new CEvent(false,false,"BufFull");
	pEmpty=new CEvent(false,false,"BufEmpty");
	pMutex =new CMutex(false,"PC_Mutex");
	CSingleLock mutexLock(pMutex);//,FullLock(pFull ),EmptyLock(pEmpty);
//	CSemaphore* m_pSemaphoreEmpty;
//	CSemaphore* m_pSemaphoreFull;
//	CMutex *m_pMutex;
//	m_pSemaphoreFull =new CSemaphore(1,1,"SemaphoreFull");
//	m_pSemaphoreEmpty =new CSemaphore(0,1,"SemaphoreEmpty");
//	m_pMutex =new CMutex(false,"PC_Mutex");//
//CSingleLock mutexLock(m_pMutex),FullLock(m_pSemaphoreFull ),EmptyLock(m_pSemaphoreEmpty );
	for(int i=0;i<100000;i++)
	{
		//EmptyLock.Lock();
		::WaitForSingleObject(pEmpty,INFINITE);
			mutexLock.Lock();
				pDlg->m_sBuf.Format("%0.10d",i);
			mutexLock.Unlock();
		pEmpty->ResetEvent();
		///pFull->SetEvent();
		//FullLock.Unlock();
		//pDlg->UpdateData(false);
	}
//	this->m_pActiveWnd->MessageBox("pr");
	return CWinThread::Run();
}

void CProducerThread::KillSelf()
{	
	if(this->m_hThread)
	{
		VERIFY( ::CloseHandle( m_hThread ) );
	}
	this->SetThreadPriority(THREAD_PRIORITY_ABOVE_NORMAL);
//	delete this;
}

⌨️ 快捷键说明

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