VC++线程通信

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

None.gif
None.gif
#define  MY_MSG WM_USER+100  // 线程通信的自定义消息
None.gif
// 消费者
None.gif
DWORD Consumer::Run()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {//创建线程并启动
InBlock.gif
    DWORD ThreadId;
InBlock.gif    m_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    
return ThreadId;//线程启动成功
ExpandedBlockEnd.gif
}

None.gif
None.gifDWORD WINAPI Consumer::Routine( LPVOID lParam)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {//线程运行函数
InBlock.gif
    std::cout<<"消费者线程启动dot.gif\n";
InBlock.gif    MSG msg;
InBlock.gif    PeekMessage(
&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)) //get msg from message queue
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            
switch(msg.message)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif            
case MY_MSG:
InBlock.gif                
//do something heredot.gif
InBlock.gif
                std::cout<<"收到消息"<<std::endl;
InBlock.gif                
break;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }
;
InBlock.gif    
return 0;
ExpandedBlockEnd.gif}

None.gif
// 生产者
None.gif
DWORD Producer::Run(LPDEALFUNCTION dealfun,LPVOID lParam)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    DWORD ThreadId;
InBlock.gif    m_DealFun 
= dealfun;
InBlock.gif    m_DealFunParam 
= NULL;
InBlock.gif    m_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    
return ThreadId;
ExpandedBlockEnd.gif}

None.gif
None.gifDWORD WINAPI Producer::Routine( LPVOID lParam )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif    Producer
& 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    
return 0;
ExpandedBlockEnd.gif}

None.gif
// main.cpp
None.gif
HANDLE hStartEvent;  //  thread start event
None.gif
unsigned nConsumerID,nProducerID;
None.gif
None.gif
void  __stdcall MyDeal( LPVOID lParam )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    std::cout
<<"开始发送消息.."<<std::endl;
InBlock.gif
InBlock.gif    
if(!PostThreadMessage(nConsumerID,MY_MSG,NULL,NULL))//post thread msg
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        printf(
"post message failed,errno:%d\n",::GetLastError());
ExpandedSubBlockEnd.gif    }

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

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

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

InBlock.gif
InBlock.gif    Producer pProducer;
InBlock.gif    nProducerID 
= pProducer.Run(&MyDeal, 0 );//启动监控线程
InBlock.gif
    if( nProducerID==-1 )
InBlock.gif        
return -1;
InBlock.gif
InBlock.gif    
return 0;
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、付费专栏及课程。

余额充值