国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
使用PostThreadMessage在Win32線程間傳遞消息_深水藍(lán)

PostThreadMessage的原型是這樣的

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

PostThreadMessage可以用于線程之間的異步通訊,因?yàn)樗挥玫却{(diào)用者返回,
這也許是線程通訊中最簡單的一種方法了。

但是要注意以下問題
1 .PostThreadMessage有時(shí)會失敗,報(bào)1444錯誤(Invalid thread identifier. )
其實(shí)這不一定是線程不存在的原因,也有可能是線程不存在消息隊(duì)列(message queue)造成的。
事實(shí)上,并不是每個(gè)thread都有message queue,那如何讓thread具有呢?
答案是,至少調(diào)用message相關(guān)的function一次,比如GetMessage,PeekMessage。

2.如果是post動態(tài)分配的memory給另外一個(gè)thread,要注意內(nèi)存的正確釋放。

3.PostThreadMessage不能夠post WM_COPYDATE之類的同步消息,否則會報(bào)錯

4.最好不要使用PostThreadMessage post message給一個(gè)窗口,使用PostMessage替代。

下面是我寫的一個(gè)比較嚴(yán)整的例子,僅供參考。

#include <windows.h>
#include
<cstdio>
#include
<process.h>

#define MY_MSG WM_USER+100
const int MAX_INFO_SIZE = 20;

HANDLE hStartEvent;
// thread start event

// thread function
unsigned __stdcall fun(void *param)
{
     printf(
"thread fun start
\n
");

     MSG msg;
     PeekMessage(
&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);

    
if(!SetEvent(hStartEvent)) //set thread start event
     {
         printf(
"set start event failed,errno:%d\n",::GetLastError());
        
return 1;
     }
    
    
while(true)
     {
        
if(GetMessage(&msg,0,0,0)) //get msg from message queue
         {
            
switch(msg.message)
             {
            
case MY_MSG:
                
char * pInfo = (char *)msg.wParam;
                 printf(
"recv %s\n",pInfo);
                 delete[] pInfo;
                
break;
             }
         }
     };
    
return 0;
}

int main()
{
     HANDLE hThread;
     unsigned nThreadID;

     hStartEvent
= ::CreateEvent(0,FALSE,FALSE,0); //create thread start event
    if(hStartEvent == 0)
     {
         printf(
"create start event failed,errno:%d\n",::GetLastError());
        
return 1;
     }

    
//start thread
     hThread = (HANDLE)_beginthreadex( NULL, 0, &fun, NULL, 0, &nThreadID );
    
if(hThread == 0)
     {
         printf(
"start thread failed,errno:%d\n",::GetLastError());
         CloseHandle(hStartEvent);
        
return 1;
     }

    
//wait thread start event to avoid PostThreadMessage return errno:1444
     ::WaitForSingleObject(hStartEvent,INFINITE);
     CloseHandle(hStartEvent);

    
int count = 0;
    
while(true)
     {
        
char* pInfo = new char[MAX_INFO_SIZE]; //create dynamic msg
         sprintf(pInfo,"msg_%d",++count);
        
if(!PostThreadMessage(nThreadID,MY_MSG,(WPARAM)pInfo,0))//post thread msg
         {
             printf(
"post message failed,errno:%d\n",::GetLastError());
             delete[] pInfo;
         }
         ::Sleep(
1000);
     }

     CloseHandle(hThread);
    
return 0;
}


本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
windows的消息隊(duì)列與消息循環(huán)
簡單的多線程編程之獲得線程參數(shù)(YC)
linux 下多線程編程
linux應(yīng)用編程之網(wǎng)絡(luò)(4)——非阻塞connect的使用及其它
Linux多線程——使用互斥量同步線程
python多線程編程(3): 使用互斥鎖同步線程
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服