WaitForSingleObject http://www.cnblogs.com/shootingstars/archive/2004/07/15/24602.html
来源:互联网 发布:php get参数加密 编辑:程序博客网 时间:2024/06/05 08:09
在主线程中慎用WaitForSingleObject (WaitForMultipleObjects)
下面的代码我调试了将近一个星期,你能够看出什么地方出了问题吗?
线程函数:
DWORD WINAPI ThreadProc(
while(!bTerminate)
{
// 从一个链表中读取信息并且插入到CListCtrl中
// CListCtrl的句柄是通过线程参数传递进来的
for(;;)
{
ReadInfoFromList();
InsertToCListCtrl();
}
}
}
主线程中使用CreateThread启动线程。while(!bTerminate)
{
// 从一个链表中读取信息并且插入到CListCtrl中
// CListCtrl的句柄是通过线程参数传递进来的
for(;;)
{
ReadInfoFromList();
InsertToCListCtrl();
}
}
}
当想终止子线程时,在主线程中:
bTerminate = TRUE;
WaitForSingleObject(threadHandle, INFINITE);
可是,以运行到WaitForSingleObject,子线程就Crash了。
为什么呢?
问题原因:
后来我终于在InsertItem的反汇编中发现了如下的代码
call dword ptr [__imp__SendMessageA@16 (7C141B54h)]
可见,InsertItem是必须借助消息循环来完成任务的。如果我们在主线程中WaitForSingleObject了,必然导致主线程阻塞,也就导致了消息循环的阻塞,最终导致工作线程Crash掉了*_*
解决方案:
为了解决在主线程中Wait的问题,微软专门设计了一个函数MsgWaitForMultipleObjects,这个函数即可以等待信号(thread,event,mutex等等),也可以等待消息(MSG)。即不论有信号被激发或者有消息到来,此函数都可以返回。呵呵,那么我的解决办法也就出来了。
将上面的WaitForSingleObject用下面的代码替换:
while(TRUE)
{
DWORD result ;
MSG msg ;
result = MsgWaitForMultipleObjects(1, &readThreadHandle,
FALSE, INFINITE, QS_ALLINPUT);
if (result == (WAIT_OBJECT_0))
{
break;
}
else
{
PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
DispatchMessage(&msg);
}
}
{
DWORD result ;
MSG msg ;
result = MsgWaitForMultipleObjects(1, &readThreadHandle,
FALSE, INFINITE, QS_ALLINPUT);
if (result == (WAIT_OBJECT_0))
{
break;
}
else
{
PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
DispatchMessage(&msg);
}
}
总结:
如果在工作线程中有可能涉及到了消息驱动的API,那么不能在主线程中使用WaitForSingleObject一类函数,而必须使用上述的方案。
分类: C++
0 0
- WaitForSingleObject http://www.cnblogs.com/shootingstars/archive/2004/07/15/24602.html
- DataGrid资料(转贴 http://www.cnblogs.com/icesnaker/archive/2004/07/31/29017.html)
- .net remoting http://www.cnblogs.com/wayfarer/archive/2004/07/30/28723.html
- 数据库视图介绍(一)(http://www.cnblogs.com/pony/archive/2008/07/15/1243210.html)
- javascript小技巧(转自DooIT)http://www.cnblogs.com/ttyp/archive/2004/11/15/63900.html
- http://www.cnblogs.com/jian1982/archive/2010/07/07/1772618.html
- http://www.cnblogs.com/chenlulouis/archive/2010/07/07/1772631.html
- HTC 编程思想(转http://www.cnblogs.com/xchunyu/archive/2004/08/05/30230.html)
- ASP常用的函数模块 http://www.cnblogs.com/cnnix/archive/2006/07/16/452286.html
- http://www.cnblogs.com/MaxWoods/archive/2007/04/07/704038.html
- .Net Remoting (转载)(转自http://www.cnblogs.com/JimmyZhang/archive/2008/07/26/1252183.html)
- .NET设计模式系列文章 http://www.cnblogs.com/Terrylee/archive/2006/07/17/334911.html
- Chart Controls (http://www.cnblogs.com/peterzb/archive/2009/07/21/1528246.html)
- http://www.cnblogs.com/yungboy/archive/2009/07/26/1531422.html
- http://www.cnblogs.com/yungboy/archive/2010/06/07/1753391.html
- http://www.cnblogs.com/yungboy/archive/2010/06/07/1753393.html
- http://www.cnblogs.com/yungboy/archive/2010/06/07/1753406.html
- HashMap按key排序(http://www.cnblogs.com/kentyshang/archive/2007/07/04/805397.html)
- cefclient 3.2345 MD/MTD编译后无法运行的问题
- bzoj4103[Thu Summer Camp 2015]异或运算
- 动态规划--最长递增子序列
- MySql中存储过程的用法
- 153.Oracle数据库SQL开发之 JAVA——导入JDBC包
- WaitForSingleObject http://www.cnblogs.com/shootingstars/archive/2004/07/15/24602.html
- 性能优化:Swap调优
- Hashset集合的运用
- 信息系统项目管理师知识要点
- mac vim 使用记录
- Android中关于修复bug的思考
- http和servlet输出
- Unity3D_NGUI_安卓APK安装包瘦身实践
- pycharm 中文