这是我现在使用的一种线程通信方式的代码片段,两个线程之间通过自定义消息进行通知,
#define
MY_MSGWM_USER+100
//
线程通信的自定义消息
//
消费者
DWORDConsumer::Run()
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{//创建线程并启动
DWORDThreadId;
m_hThread=CreateThread(NULL,0,Routine,0,0,&ThreadId);//创建更新线程
if(NULL==m_hThread)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
return-1;//启动失败
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
returnThreadId;//线程启动成功
}
![None.gif](/Images/OutliningIndicators/None.gif)
DWORDWINAPIConsumer::Routine(LPVOIDlParam)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{//线程运行函数
std::cout<<"消费者线程启动
/n";
MSGmsg;
PeekMessage(&msg,NULL,WM_USER,WM_USER,PM_NOREMOVE);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
while(true)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
if(GetMessage(&msg,0,0,0))//getmsgfrommessagequeue
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
switch(msg.message)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
caseMY_MSG:
//dosomethinghere![dot.gif](/Images/dot.gif)
std::cout<<"收到消息"<<std::endl;
break;
}
}
};
return0;
}
//
生产者
DWORDProducer::Run(LPDEALFUNCTIONdealfun,LPVOIDlParam)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
DWORDThreadId;
m_DealFun=dealfun;
m_DealFunParam=NULL;
m_hThread=CreateThread(NULL,0,Routine,this,0,&ThreadId);//创建监控线程
if(NULL==m_hThread)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
return-1;
}
returnThreadId;
}
![None.gif](/Images/OutliningIndicators/None.gif)
DWORDWINAPIProducer::Routine(LPVOIDlParam)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
Producer&obj=*(Producer*)lParam;//生产者对象
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
while(!obj.m_bRequestStop)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{//线程就不断运行直到收到用户的退出请求
obj.m_DealFun(obj.m_DealFunParam);
}
return0;
}
//
main.cpp
HANDLEhStartEvent;
//
threadstartevent
unsignednConsumerID,nProducerID;
![None.gif](/Images/OutliningIndicators/None.gif)
void
__stdcallMyDeal(LPVOIDlParam)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
std::cout<<"开始发送消息.."<<std::endl;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
if(!PostThreadMessage(nConsumerID,MY_MSG,NULL,NULL))//postthreadmsg
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
printf("postmessagefailed,errno:%d/n",::GetLastError());
}
::Sleep(1000);
}
int
main()
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
Consumerconsumer;
nConsumerID=consumer.Run();//启动更新线程
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
if(nConsumerID==-1)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
std::cout<<"startthreadfailed"<<std::endl;
return-1;
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
ProducerpProducer;
nProducerID=pProducer.Run(&MyDeal,0);//启动监控线程
if(nProducerID==-1)
return-1;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
return0;
}
![None.gif](/Images/OutliningIndicators/None.gif)
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />