用MsgWaitForMultipleObjects代替WaitForSingleObject和WaitForMultipleObjects()
来源:互联网 发布:西北师范知行教务管理 编辑:程序博客网 时间:2024/06/06 08:37
在多线程编程中,通常都需要线程间的同步,一个线程要等待另一个线程的事件才继续执行,一般的做法是采用WaitForSingleObject和WaitForMultipleObjects()函数来实现。
但在实际的应用中,经常出现等待线程卡死的状况,也就是说等待的事件一直无效。为什么事件一直无效呢?很多的情况是等待线程阻塞了另外的线程,使另外的线程无法设置事件有效。为什么会阻塞呢?原因就比较多了,需要具体问题具体分析。
WaitForSingleObject和WaitForMultipleObjects()都是阻塞函数,事件无效就一直不返回,从而阻塞该线程,使该线程无法处理其他的事务,如果其他的线程发送消息过来,将得不到处理而不返回,从而将其他的线程也阻塞,造成相互等待,这就是臭名昭著的“死锁”!!!
微软提供了另外一个函数可以解决该问题,它就是MsgWaitForMultipleObjects()函数,该函数不但可以等待事件,还可以等待消息,从而处理消息,使线程不阻塞。该函数的具体解释前参考MSDN或网络。
一般的使用方法为:
- DWORD dwRet = 0;
- MSG msg;
- DWORD dwStartTime = GetTickCount();
- while (TRUE)
- {
- //超时判断 5s
- dwRet = GetTickCount() - dwStartTime;
- if ((GetTickCount() - dwStartTime) > 10000)
- {
- AfxMessageBox(_T("获取数据超时,请检测设备网络连接!"), MB_OK | MB_ICONERROR);
- return NULL;
- }
- //wait for m_hThread to be over,and wait for
- //QS_ALLINPUT(Any message is in the queue)
- //dwRet = WaitForSingleObject(g_hRetEvent, INFINITE);
- dwRet = MsgWaitForMultipleObjects (1, &g_hRetEvent, FALSE, 100, QS_ALLINPUT);
- switch(dwRet)
- {
- case WAIT_OBJECT_0: //返回数据达到
- break; //break the loop
- case WAIT_OBJECT_0 + 1: //界面消息
- //get the message from Queue
- //and dispatch it to specific window
- if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- continue;
- case WAIT_TIMEOUT: //超时
- continue;
- default:
- AfxMessageBox(_T("数据获取失败,未知错误!"), MB_OK | MB_ICONERROR);
- return NULL;
- break; // unexpected failure
- }
- break;
- }
特别是在主线程和界面线程中推荐使用该函数,可以避免很多麻烦!!!
阅读全文
0 0
- 用MsgWaitForMultipleObjects代替WaitForSingleObject和WaitForMultipleObjects()
- 在用户线程/主线程中推荐MsgWaitForMultipleObjects代替WaitForSingleObject和WaitForMultipleObjects()函数
- 在用户线程/主线程中推荐MsgWaitForMultipleObjects代替WaitForSingleObject和WaitForMultipleObjects()函数
- 在主线程中慎用WaitForSingleObject (WaitForMultipleObjects) --代替方法:MsgWaitForMultipleObjects
- 在主线程中慎用WaitForSingleObject (WaitForMultipleObjects) --代替方法:MsgWaitForMultipleObjects
- WaitForSingleObject和MsgWaitForMultipleObjects
- Sleep和WaitForSingleObject ,WaitForMultipleObjects
- Sleep和WaitForSingleObject ,WaitForMultipleObjects
- WaitForSingleObject和WaitForMultipleObjects用法
- WaitForSingleObject和 WaitForMultipleObjects
- WaitForSingleObject和WaitForMultipleObjects用法
- WaitForSingleObject 和 WaitForMultipleObjects
- WaitForSingleObject和WaitForMultipleObjects
- WaitForSingleObject 和 WaitForMultipleObjects函数
- WaitForSingleObject 和 WaitForMultipleObjects函数
- WaitForSingleObject 和 WaitForMultipleObjects函数
- WaitForSingleObject 和 WaitForMultipleObjects函数
- MsgWaitForMultipleObjects WaitForMultipleObjects
- 每日AC--leetcode--recordList
- HDU 5361 In Touch(并查集实现区间删除)
- vim编辑器-多行加注释与去注释
- Jquery 操作Html 控件 CheckBox、Radio、Select 控件
- dubbo 框架学习-将dubbo 服务做成可执行的java 应用
- 用MsgWaitForMultipleObjects代替WaitForSingleObject和WaitForMultipleObjects()
- 迷茫
- 每日随笔_Animation_Animator
- HDOJ3594-Cactus(仙人掌图)
- java代码中init method和destroy method的三种使用方式
- SpringMvc面试题
- 程序员装逼指南
- 莫比乌斯反演小结
- HTML 网页特殊符号HTML代码大全