源码:
链接: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();
}