使用PostThreadMessage在Win32线程间传递消息

  1. #include <windows.h>
  2. #include <cstdio>
  3. #include <process.h>
  4. #define MY_MSG WM_USER+100
  5. const int MAX_INFO_SIZE = 20;
  6. HANDLE hStartEvent; // thread start event
  7. // thread function
  8. unsigned __stdcall fun(void *param)
  9. {
  10.     printf("thread fun start/n");
  11.     MSG msg;
  12.     PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
  13.     if(!SetEvent(hStartEvent)) //set thread start event 
  14.     {
  15.         printf("set start event failed,errno:%d/n",::GetLastError());
  16.         return 1;
  17.     }
  18.     
  19.     while(true)
  20.     {
  21.         if(GetMessage(&msg,0,0,0)) //get msg from message queue
  22.         {
  23.             switch(msg.message)
  24.             {
  25.             case MY_MSG:
  26.                 char * pInfo = (char *)msg.wParam;
  27.                 printf("recv %s/n",pInfo);
  28.                 delete[] pInfo;
  29.                 break;
  30.             }
  31.         }
  32.     };
  33.     return 0;
  34. }
  35. int main()
  36. {
  37.     HANDLE hThread;
  38.     unsigned nThreadID;
  39.     hStartEvent = ::CreateEvent(0,FALSE,FALSE,0); //create thread start event
  40.     if(hStartEvent == 0)
  41.     {
  42.         printf("create start event failed,errno:%d/n",::GetLastError());
  43.         return 1;
  44.     }
  45.     //start thread
  46.     hThread = (HANDLE)_beginthreadex( NULL, 0, &fun, NULL, 0, &nThreadID );
  47.     if(hThread == 0)
  48.     {
  49.         printf("start thread failed,errno:%d/n",::GetLastError());
  50.         CloseHandle(hStartEvent);
  51.         return 1;
  52.     }
  53.     //wait thread start event to avoid PostThreadMessage return errno:1444
  54.     ::WaitForSingleObject(hStartEvent,INFINITE);
  55.     CloseHandle(hStartEvent);
  56.     int count = 0;
  57.     while(true)
  58.     {
  59.         char* pInfo = new char[MAX_INFO_SIZE]; //create dynamic msg
  60.         sprintf(pInfo,"msg_%d",++count);
  61.         if(!PostThreadMessage(nThreadID,MY_MSG,(WPARAM)pInfo,0))//post thread msg
  62.         {
  63.             printf("post message failed,errno:%d/n",::GetLastError());
  64.             delete[] pInfo;
  65.         }
  66.         ::Sleep(1000);
  67.     }
  68.     CloseHandle(hThread);
  69.     return 0;
  70. }
 

PostThreadMessage的原型是这样的

BOOL PostThreadMessage( DWORD idThread,
    UINT Msg,
    WPARAM wParam,
    LPARAM lParam
);

PostThreadMessage可以用于线程之间的异步通讯,因为它不用等待调用者返回,
这也许是线程通讯中最简单的一种方法了。

但是要注意以下问题
1 .PostThreadMessage有时会失败,报1444错误(Invalid thread identifier. )
其实这不一定是线程不存在的原因,也有可能是线程不存在消息队列(message queue)造成的。
事实上,并不是每个thread都有message queue,那如何让thread具有呢?
答案是,至少调用message相关的function一次,比如GetMessage,PeekMessage。

2.如果是post动态分配的memory给另外一个thread,要注意内存的正确释放。

3.PostThreadMessage不能够post WM_COPYDATE之类的同步消息,否则会报错

4.最好不要使用PostThreadMessage post message给一个窗口,使用PostMessage替代。

下面是我写的一个比较严整的例子,仅供参考。


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值