使用临界区对象实现读,写线程同步

本文介绍了一个使用CCriticalSection实现线程同步的例子。该例子通过创建读写两个工作线程,并利用临界区对象确保线程安全地访问共享资源。文章详细展示了如何定义线程函数、启动及停止线程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本例创建两个工作线程,通过使用CCriticalSection类对象,保证同一时刻只有一个线程可以访问临界区对象。

开发过程:

(1) .界面:

(2) 在对话框类的实现文件的顶部定义全局CCriticalSection对象,全局资源和工作线程入口函数

CCriticalSection g_cs;       //临界区对象
int g_data = 0;              //全局资源
static HANDLE g_ReadHnd;     //读线程句柄
static HANDLE g_WriteHnd;    //写线程句柄
//用于写数据的线程,即第一个线程的回调函数
UINT ThreadProcWrite(LPVOID param)
{
	CEdit *pedit = (CEdit*)param;
	while(TRUE)
	{
		CString str;
		g_cs.Lock();                      //临界区锁定共享资源
		g_data++;                         //数据加1
		str.Format("%d",g_data);
		pedit->SetWindowText(str);        //编辑框显示数据
		Sleep(1000);                      //阻塞线程一段时间
		g_cs.Unlock();                    //临界区解锁
	}
	return 0;
}

//用于读数据的线程,即第二个线程的回调函数
UINT ThreadProcRead(LPVOID param)
{
	UINT retval;
	CEdit *pedit = (CEdit*)param;
	while(TRUE)
	{
		g_cs.Lock();                      //临界区锁定共享资源
		retval = g_data;                  //读数据
		g_cs.Unlock();                    //解锁
		CString str;
		str.Format("%d",retval);
		pedit->SetWindowText(str);        //编辑框显示数据

	}
	return 0;
}

(3)启动和停止写,读线程实现代码

void CCriticalSectionThreadSynDlg::OnStarw()                //启动写线程 
{ 
	// TODO: Add your control notification handler code here
	CWinThread* pThread;                                      
	pThread = AfxBeginThread(ThreadProcWrite, &m_WriteEdit);   //开始写线程
	g_WriteHnd = pThread->m_hThread;                           //获取写线程句柄
	GetDlgItem(IDC_STARW)->EnableWindow(FALSE);                //"启动"按钮无效
	GetDlgItem(IDC_STOPW)->EnableWindow(TRUE);                 //"停止"按钮生效
}

void CCriticalSectionThreadSynDlg::OnStarr()               //启动读线程
{
	// TODO: Add your control notification handler code here
	CWinThread* pThread;
	pThread = AfxBeginThread(ThreadProcRead, &m_ReadEdit);   //开始读线程
	g_ReadHnd = pThread->m_hThread;                          //获取读线程句柄
	GetDlgItem(IDC_STARR)->EnableWindow(FALSE);              //"启动"按钮无效
	GetDlgItem(IDC_STOPR)->EnableWindow(TRUE);               //"停止"按钮生效
}

void CCriticalSectionThreadSynDlg::OnStopr() 
{
	// TODO: Add your control notification handler code here
	g_cs.Lock();                                       //锁定资源
	TerminateThread(g_ReadHnd,0);                      //终止读线程
	g_cs.Unlock();                                     //释放资源
	m_ReadEdit.SetWindowText("终止读线程");      
	GetDlgItem(IDC_STARR)->EnableWindow(TRUE);
	GetDlgItem(IDC_STOPR)->EnableWindow(FALSE);
}

void CCriticalSectionThreadSynDlg::OnStopw() 
{
	// TODO: Add your control notification handler code here
	g_cs.Lock();                                  //锁定资源
	TerminateThread(g_WriteHnd,0);                //终止读线程
	g_cs.Unlock();                                //释放资源
	m_WriteEdit.SetWindowText("终止写线程");
	GetDlgItem(IDC_STARW)->EnableWindow(TRUE);
	GetDlgItem(IDC_STOPW)->EnableWindow(FALSE);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值