少莫千华

入网以博客,存世之芳泽

【语言-C++】多线程通同步 临界区 CCriticalSection 与 CSingleLock

多线程通同步与互斥示例

下面示例是一个相机处理和显示分开的两个线程:

定义临界区使用单锁

#define _CRITICAL_LOCK(critical_lock)			CSingleLock locker(&critical_lock);	locker.Lock();
CCriticalSection _critical_data2;

启动线程,创建四个事件:停止线程事件、处理图像事件、显示图像事件和接收图像事件

停止线程事件:线程接受自动复位 ,初始状态为无信号状态;控制两个线程同时结束

处理图像事件:手动复位,初始状态为无信号状态;

显示图像事件:手动复位,初始状态为无信号状态;

接收图像事件:手动复位,初始状态为有信号状态;

处理图像事件和显示图像事件同时发生时,激活图像处理线程,进行图像处理;接收图像事件的时间的触发由显示图像完成后触发;处理图像事件由相机线程图像传输来触发;

显示图像事件发生时激活显示图像线程进行图像处理。

创建线程

void CMainDlgEx::StartThread()
{
	m_EventStop			= CreateEvent(NULL,TRUE,FALSE,NULL);
	m_EventDealImage		= CreateEvent(NULL,FALSE,FALSE,NULL);
	m_EventImageShow		 = CreateEvent(NULL,FALSE,FALSE,NULL);
	m_EventAllowReceiveImage = CreateEvent(NULL,FALSE,TRUE,NULL);
	// auxiliary methods
	ResetEvent(m_EventStop);
	SetEvent(m_EventAllowReceiveImage);
	// 启动三个线程 
	m_HTDealImage = (HANDLE)_beginthreadex(NULL,								
		0,								
		(unsigned (__stdcall *)(void*))	&Thread_DealImage,
		this,					
		0,					
		&m_IDDealImage);			

	m_HTShowImage = (HANDLE)_beginthreadex(NULL,					
		0,					
		(unsigned (__stdcall *)(void*))&Thread_ShowImage,	
		this,					
		0,					
		&m_IDShowImage);					
}


关闭线程

void CMainDlgEx::CloseThread()
{  
	SetEvent(m_EventStop);       
	WaitForSingleObject(m_HTShowImage,INFINITE);
	WaitForSingleObject(m_HTDealImage,INFINITE);
	if (m_EventStop!=NULL &&m_EventStop !=INVALID_HANDLE_VALUE)
		CloseHandle(m_EventStop);
	if (m_EventDealImage!=NULL &&m_EventDealImage !=INVALID_HANDLE_VALUE)
		CloseHandle(m_EventDealImage);
	if (m_EventAllowReceiveImage!=NULL &&m_EventAllowReceiveImage !=INVALID_HANDLE_VALUE)
		CloseHandle(m_EventAllowReceiveImage );
	if (m_EventImageShow!=NULL &&m_EventImageShow !=INVALID_HANDLE_VALUE)
		CloseHandle(m_EventImageShow);
	DeleteCriticalSection(&m_DealImageMutex);
}


处理线程

void Thread_DealImage(LPVOID *lpParam)
{
	OutputDebugString(_T("处理中 ....启动\n"));
	HANDLE      eventHandle[2];
	CMainDlgEx *MultiThrDlg = (CMainDlgEx*) lpParam;	

	eventHandle[0] = (*MultiThrDlg).m_EventAllowReceiveImage; 
	eventHandle[1] = (*MultiThrDlg).m_EventDealImage;  

	while (WAIT_OBJECT_0 != WaitForSingleObject((MultiThrDlg->m_EventStop),0))
	{
		//抓取数据到grabData中
		while (WAIT_OBJECT_0 == WaitForMultipleObjects(2,eventHandle,TRUE,0))
		{
			_CRITICAL_LOCK(_critical_data2);
			OutputDebugString(_T("处理中 ....\n"));
			SetEvent(MultiThrDlg->m_EventImageShow);
		}
	}
	ResetEvent(MultiThrDlg->m_EventDealImage);
	ResetEvent(MultiThrDlg->m_EventAllowReceiveImage);
	OutputDebugString(_T("处理中 ....退出\n"));
	return;
}


显示线程

extern void Thread_ShowImage( LPVOID *lpParam )
{
	HANDLE      eventHandle[2];
	CMainDlgEx *MultiThrDlg = (CMainDlgEx*) lpParam;	
	OutputDebugString(_T("显示中 ....启动\n"));
	while (WAIT_OBJECT_0 != WaitForSingleObject((MultiThrDlg->m_EventStop),0))
	{
		//抓取数据到grabData中
		while (WAIT_OBJECT_0 == WaitForSingleObject((MultiThrDlg->m_EventImageShow),0))
		{
			_CRITICAL_LOCK(_critical_data2);
			OutputDebugString(_T("显示中 ....\n"));
			SetEvent(MultiThrDlg->m_EventAllowReceiveImage);
		}
	}
	ResetEvent(MultiThrDlg->m_EventImageShow);
	ResetEvent(MultiThrDlg->m_EventAllowReceiveImage);
	OutputDebugString(_T("显示中 ....退出\n"));
	return;
}

数据到达

LRESULT CMainDlgEx::OnCameraDataArrive(WPARAM w, LPARAM l)	
{
	SetEvent(m_EventDealImage);
	return -1;
}




阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chenlu5201314/article/details/53996227
个人分类: 语言-C++
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭