首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

关于线程代码执行完了,但线程未退出,也未激发WaitForSingleObject的有关问题

2012-01-24 
关于线程代码执行完了,但线程未退出,也未激发WaitForSingleObject的问题。RT,具体代码如下:C/C++ codeBOOL

关于线程代码执行完了,但线程未退出,也未激发WaitForSingleObject的问题。
RT,具体代码如下:

C/C++ code
BOOL CMsgQueue::StopRevMsgThread(void)  {      if(m_hRevMsgThread)      {          //m_bThreadExit = TRUE;                CMsgQueue cMsgQueue(L"MsgQueueTest",1024,WriteMode);          // 往消息队列写数据          cMsgQueue.Write("__EXIT__",9);          cMsgQueue.Close();          // 等待线程成功退出          WaitForSingleObject(m_hRevMsgThread,INFINITE);          CloseHandle(m_hRevMsgThread);          m_hRevMsgThread = NULL;          m_hRevMsgThread = FALSE;        lpBuffer?free(lpBuffer),lpBuffer=0:0;    }      return ((m_hRevMsgThread==NULL) ? TRUE : FALSE);  }    DWORD WINAPI CMsgQueue::RevMsgThread(LPVOID pParam)  {       CMsgQueue *pMsgQueue=(CMsgQueue*)pParam;     DWORD dwReadNums=0;       while(!pMsgQueue->m_bThreadExit)       {           if(!pMsgQueue->m_hMsgQueue )               break;           // 从消息队列中读取一条消息(阻塞模式)           BOOL ret=pMsgQueue->Read(pMsgQueue->lpBuffer,pMsgQueue->m_dwQueueSize,&dwReadNums,INFINITE);           printf("Read ret=%d,dwReadNums=%d/n",ret,dwReadNums);           if(dwReadNums>0)           {               // 调用回调函数             if(strcmp("__EXIT__",(char*)pMsgQueue->lpBuffer)){                if(pMsgQueue->m_MsgCallBack)                      pMsgQueue->m_MsgCallBack(pMsgQueue->lpBuffer,dwReadNums);               }else{                break;             }         }       }     printf("RevMsgThread exit...\r\n");     ::ExitThread(0);     return 0;}  

debug窗口出来的内容:
[VPU DEC]Interlace : 0
[VPU DEC]Crop : 0,0,0,0
================================================
Read ret=1,dwReadNums=79/n[dll] PLAYER_THREAD_ATTACH
Read ret=1,dwReadNums=9/nRevMsgThread exit...

可以看到RevMsgThread exit...消息打印了,本应显示线程退出的提示的,并激发WaitForSingleObject继续运行,但这里线程一直没有退出。。

[解决办法]
有点疑惑...m_hRevMsgThread对不?
你的cMsgQueue是个局部的东西,和线程函数里的param是一个东西吗?
[解决办法]
把这个去掉::ExitThread(0);
[解决办法]
没有跑,不过建议把cMsgQueue.Close()和 WaitForSingleObject(m_hRevMsgThread,INFINITE)换下位置试一下
[解决办法]
如果是win7下面的话 用WaitForSingleObject(m_hRevMsgThread,INFINITE);
这种方式来判断线程是否结束 是不行的.而xp是可以的...很早前我发现了,但是无解.
比如你用远程线程注入一个dll,然后等待远程线程执行完并关闭线程句柄....
会发现WaitForSingleObject 完全无效
[解决办法]
代码太多没有仔细看
你最好确保句柄在WaitForSingleObject时是有效的
Handle to the object. For a list of the object types whose handles can be specified, see the following Remarks section. 
If this handle is closed while the wait is still pending, the function's behavior is undefined.

热点排行