这是我现在使用的一种线程通信方式的代码片段,两个线程之间通过自定义消息进行通知,
#define MY_MSG WM_USER+100 //线程通信的自定义消息
m_hThread = CreateThread( NULL,0,Routine,0,0,&ThreadId );//创建更新线程
DWORD WINAPI Consumer::Routine( LPVOID lParam)
PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
if(GetMessage(&msg,0,0,0)) //get msg from message queue
std::cout<<"收到消息"<<std::endl;
DWORD Producer::Run(LPDEALFUNCTION dealfun,LPVOID lParam)
m_hThread = CreateThread( NULL,0,Routine,this,0,&ThreadId );//创建监控线程
DWORD WINAPI Producer::Routine( LPVOID lParam )
Producer& obj = *(Producer*)lParam;//生产者对象
while( !obj.m_bRequestStop )
obj.m_DealFun(obj.m_DealFunParam );
HANDLE hStartEvent; // thread start event
unsigned nConsumerID,nProducerID;
void __stdcall MyDeal( LPVOID lParam )
std::cout<<"开始发送消息.."<<std::endl;
if(!PostThreadMessage(nConsumerID,MY_MSG,NULL,NULL))//post thread msg
printf("post message failed,errno:%d\n",::GetLastError());
nConsumerID = consumer.Run();//启动更新线程
std::cout<<"start thread failed"<<std::endl;
nProducerID = pProducer.Run(&MyDeal, 0 );//启动监控线程
本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2008/03/14/1106481.html,如需转载请自行联系原作者