C++ MFC 线程同步(队列、互斥)生产者==>消费者

源码:
链接:https://pan.baidu.com/s/13uPsHDoePCWOZlDYjOfekw
提取码:f4ox

1、定义数据类

// 数据信息
class CPersonInfo
{
public:
	CPersonInfo(const CString strName, int nAge)
	{
		this->strName = strName;
		this->nAge = nAge;
	}

	CString strName;
	int nAge;
public:
	CString GetInfo();
};
// 获取信息
CString CPersonInfo::GetInfo()
{
	CString strAge;
	strAge.Format("%d", this->nAge);
	CString strInfo = this->strName + " " + strAge;
	return strInfo;
}

2、定义互斥对象 / 数据队列

	HANDLE  m_hMutex = 0; //  互斥对象
    std::deque<CPersonInfo> m_dqPersonList;  // 数据队列
	// 初始化互斥句柄
	m_hMutex = CreateMutexW(NULL, FALSE, NULL);

3、定义发送接受线程

	// 发送线程处理函数
	static UINT  ProcSendThread(LPVOID pParam);
	// 接受线程处理函数
	static UINT  ProcRcvThread(LPVOID pParam);

发送线程

// 发送线程处理函数
UINT  CThreadSynDlg::ProcSendThread(LPVOID pParam)
{
	CThreadSynDlg * pThis = (CThreadSynDlg*)pParam;

	CString  nameList[3] = { "张三", "李四", "王五" };
	int nCount = 0;

	while (TRUE)
	{
		// 等待结束事件
		if (WaitForSingleObject(pThis->m_procEvent.m_hObject, 5) == WAIT_OBJECT_0)
		{
			// 重置事件
			ResetEvent(pThis->m_procEvent);
			break;
		}
		
		// 创建数据
		CString strCount;
		strCount.Format("%05d", nCount);
		CPersonInfo imgInfo(nameList[nCount%3], 20+nCount);

		WaitForSingleObject(pThis->m_hMutex, INFINITE);  // 互斥句柄

		// 数据添加队列
		pThis->m_dqPersonList.push_back(imgInfo);
		pThis->m_lbThreadSend.AddString("添加>>队列 " + imgInfo.GetInfo());

		ReleaseMutex(pThis->m_hMutex); // 释放句柄

		// 显示处理日志
		int count = pThis->m_lbThreadSend.GetCount();
		pThis->m_lbThreadSend.SetCurSel(count - 1);

		Sleep(500);
		nCount++;
	}

	// 线程结束
	pThis->m_bStarted = FALSE;

	pThis->m_lbThreadSend.AddString(_T("-----------------线程结束--------------------"));
	int count = pThis->m_lbThreadSend.GetCount();
	pThis->m_lbThreadSend.SetCurSel(count - 1);

	return 0;
}

接受线程


// 接受线程
UINT  CThreadSynDlg::ProcRcvThread(LPVOID pParam)
{
	CThreadSynDlg * pThis = (CThreadSynDlg*)pParam;

	int nCount = 0;
	while (TRUE)
	{
		// 等待结束事件
		if (WaitForSingleObject(pThis->m_procRcvEvent.m_hObject, 5) == WAIT_OBJECT_0)
		{
			// 重置事件
			ResetEvent(pThis->m_procRcvEvent);
			break;
		}

		// 读取队列是否空
		if (pThis->m_dqPersonList.empty())
		{
			// 空队列
			Sleep(5);
			continue;
		}

		WaitForSingleObject(pThis->m_hMutex, INFINITE);  // 等待互斥句柄

		// 读取 队列数据
		CPersonInfo personInfo= pThis->m_dqPersonList.front();
		pThis->m_dqPersonList.pop_front();

		ReleaseMutex(pThis->m_hMutex); // 释放互斥句柄

		// 显示处理日志
		pThis->m_lbThreadRcv.AddString("取队列<< " + personInfo.GetInfo());
		int count = pThis->m_lbThreadRcv.GetCount();
		pThis->m_lbThreadRcv.SetCurSel(count - 1);

		Sleep(10);
		nCount++;
	}

	// 线程结束
	pThis->m_bStarted = FALSE;

	pThis->m_lbThreadRcv.AddString(_T("-----------------线程结束--------------------"));
	int count = pThis->m_lbThreadRcv.GetCount();
	pThis->m_lbThreadRcv.SetCurSel(count - 1);

	return 0;
}

4、启动线程

// 启动线程
void CThreadSynDlg::OnBnClickedBtnStart()
{
	if (m_bStarted)
	{// 线程已经启动
		m_lbThreadSend.AddString(_T("E01--线程已经启动=========================\n"));
		return;
	}

	m_lbThreadSend.AddString(_T("启动线程*********\n"));
	m_lbThreadRcv.AddString(_T("启动线程*********\n"));

	// 启动线程, 传递对话框指针
	AfxBeginThread(ProcSendThread, this);
	AfxBeginThread(ProcRcvThread, this);
	m_bStarted = TRUE;

}

5、停止线程

// 停止发送线程
void CThreadSynDlg::OnBnClickedBtnStopSend()
{
	// 写入日志
	m_lbThreadSend.AddString(_T("结束线程-------------\n"));
	int count = m_lbThreadSend.GetCount();
	m_lbThreadSend.SetCurSel(count - 1);

	// 结束发送线程事件
	m_procEvent.SetEvent();
}

// 停止接受线程
void CThreadSynDlg::OnBnClickedBtnStopRcv()
{
	m_lbThreadRcv.AddString(_T("结束线程-------------\n"));
	int count = m_lbThreadRcv.GetCount();
	m_lbThreadRcv.SetCurSel(count - 1);

	// 设置线程事件
	m_procRcvEvent.SetEvent();
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

廷益--飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值