注册 登录
编程论坛 VC++/MFC

向大家请教一个关于多线程互斥的问题,望不吝赐教,十分感谢!

hk_zgy 发布于 2014-10-19 22:28, 476 次点击
DWORD WINAPI ThreadProc18(LPVOID pthread)
 {
 while(1)
 {
 WaitForSingleObject(hMutex,INFINITE);
 for (int i=0;i<10;i++)
 {
 CWnd * pWnd;
 pWnd =CWnd::FindWindow ( NULL, "UDP_test" );//UDP_test为对话框标题
CString str;
 str.Format("%d",i);
 pWnd->SetDlgItemText(IDC_EDIT1_xiancheng2,str);
 Sleep(100);
 }
 global_count++;
 ReleaseMutex(hMutex);
 }
 }
 DWORD WINAPI ThreadProc19(LPVOID pthread)
 {
 while(1)
 {
 WaitForSingleObject(hMutex,INFINITE);
 for (int i=0;i<10;i++)
 {
 CWnd * pWnd;
 pWnd =CWnd::FindWindow ( NULL, "UDP_test" );//UDP_test为对话框标题
CString str;
 str.Format("%d",i);
 pWnd->SetDlgItemText(IDC_EDIT1_xiancheng,str);
 Sleep(100);
 }
 global_count++;
 ReleaseMutex(hMutex);
 }

 }
程序中还使用了WSAAsyncSelect(m_socket_net,m_hWnd,UM_SOCK,FD_READ),socket UDP通信,在
 case FD_READ:
 ...
                    WaitForSingleObject(hMutex,INFINITE);
 global_count++;
 ReleaseMutex(hMutex);
         break;
程序运行后,上面的两个线程工作正常,交替增加并显示,但是当接收到网络数据时,程序停在case FD_READ的WaitForSingleObject(hMutex,INFINITE);处,所有线程停止,程序死了,请问是怎么回事?十分感谢!
1 回复
#2
天使梦魔2014-10-31 13:04
不知道你怎么设计其他的,WaitForSingleObject去百度下是做什么用的再来讨论


你这样设计线程不适用消息泵么,居然用死循环一直执行,不怕计算拖死其它线程么(我说这个和你那个无关)
1