VC++线程通信

这是我现在使用的一种线程通信方式的代码片段,两个线程之间通过自定义消息进行通知,

None.gif
None.gif
#define MY_MSGWM_USER+100 // 线程通信的自定义消息
None.gif
// 消费者
None.gif
DWORDConsumer::Run()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {//创建线程并启动
InBlock.gif
DWORDThreadId;
InBlock.gifm_hThread
=CreateThread(NULL,0,Routine,0,0,&ThreadId);//创建更新线程
InBlock.gif
if(NULL==m_hThread)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
return-1;//启动失败
ExpandedSubBlockEnd.gif
}

InBlock.gif
InBlock.gif
returnThreadId;//线程启动成功
ExpandedBlockEnd.gif
}

None.gif
None.gifDWORDWINAPIConsumer::Routine(LPVOIDlParam)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {//线程运行函数
InBlock.gif
std::cout<<"消费者线程启动dot.gif/n";
InBlock.gifMSGmsg;
InBlock.gifPeekMessage(
&msg,NULL,WM_USER,WM_USER,PM_NOREMOVE);
InBlock.gif
InBlock.gif
while(true)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
if(GetMessage(&msg,0,0,0))//getmsgfrommessagequeue
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
switch(msg.message)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
caseMY_MSG:
InBlock.gif
//dosomethingheredot.gif
InBlock.gif
std::cout<<"收到消息"<<std::endl;
InBlock.gif
break;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}
;
InBlock.gif
return0;
ExpandedBlockEnd.gif}

None.gif
// 生产者
None.gif
DWORDProducer::Run(LPDEALFUNCTIONdealfun,LPVOIDlParam)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifDWORDThreadId;
InBlock.gifm_DealFun
=dealfun;
InBlock.gifm_DealFunParam
=NULL;
InBlock.gifm_hThread
=CreateThread(NULL,0,Routine,this,0,&ThreadId);//创建监控线程
InBlock.gif
if(NULL==m_hThread)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
return-1;
ExpandedSubBlockEnd.gif}

InBlock.gif
returnThreadId;
ExpandedBlockEnd.gif}

None.gif
None.gifDWORDWINAPIProducer::Routine(LPVOIDlParam)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifProducer
&obj=*(Producer*)lParam;//生产者对象
InBlock.gif

InBlock.gif
while(!obj.m_bRequestStop)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{//线程就不断运行直到收到用户的退出请求
InBlock.gif
obj.m_DealFun(obj.m_DealFunParam);
ExpandedSubBlockEnd.gif}

InBlock.gif
return0;
ExpandedBlockEnd.gif}

None.gif
// main.cpp
None.gif
HANDLEhStartEvent; // threadstartevent
None.gif
unsignednConsumerID,nProducerID;
None.gif
None.gif
void __stdcallMyDeal(LPVOIDlParam)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifstd::cout
<<"开始发送消息.."<<std::endl;
InBlock.gif
InBlock.gif
if(!PostThreadMessage(nConsumerID,MY_MSG,NULL,NULL))//postthreadmsg
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifprintf(
"postmessagefailed,errno:%d/n",::GetLastError());
ExpandedSubBlockEnd.gif}

InBlock.gif::Sleep(
1000);
ExpandedBlockEnd.gif}

None.gif
int main()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifConsumerconsumer;
InBlock.gifnConsumerID
=consumer.Run();//启动更新线程
InBlock.gif

InBlock.gif
if(nConsumerID==-1)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifstd::cout
<<"startthreadfailed"<<std::endl;
InBlock.gif
return-1;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gifProducerpProducer;
InBlock.gifnProducerID
=pProducer.Run(&MyDeal,0);//启动监控线程
InBlock.gif
if(nProducerID==-1)
InBlock.gif
return-1;
InBlock.gif
InBlock.gif
return0;
ExpandedBlockEnd.gif}

None.gif
None.gif

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值