WaitForSingleObject()返回值造成的bug

江枫思渺然 提交于 2020-02-29 10:02:24

<p>代码如下:</p> <pre class="csharpcode">AppDataPromulgator *pThis = (AppDataPromulgator *)arg; HANDLE eventHandeArr[2] = { pThis-&gt;m_exitDistributeRealData, s_realDataTransParam.updataEvent }; <span class="kwrd">while</span> ( 1 ) { DWORD ret = ::WaitForMultipleObjects( 2, eventHandeArr, FALSE, INFINITE ); <span class="kwrd">if</span> ( WAIT_OBJECT_0 == ret ) { <span class="kwrd">break</span>; } <span class="kwrd">else</span> <span class="kwrd">if</span> ( WAIT_OBJECT_0+1 == ret ) { <span class="kwrd">if</span> (NULL == s_realDataTransParam.CurrFreq) { <span class="kwrd">continue</span>; } DWORD freqTemp = *(s_realDataTransParam.CurrFreq); <span class="kwrd">float</span> currFreq = <span class="kwrd">float</span>(FREQ_FACTOR) / <span class="kwrd">float</span>(freqTemp+1); <span class="kwrd">if</span>( currFreq &lt; MIN_FREQ || currFreq &gt; MAX_FREQ ) { currFreq = 0.0f; } pThis-&gt;DistributeRealData( s_realDataTransParam.pRecData, currFreq ); } } RETAILMSG( 1, (_T(<span class="str">&quot; AppDataPromulgator::RealDataDistributeThread() Exit\r\n&quot;</span>) ) ); <span class="kwrd">return</span> 0;</pre> <style type="text/css"> .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }</style>

<p>其错误原因主要如下:</p>

<p>当s_realDataTransParam.updataEvent 为无效值时,则WaitForMultipleObjects()既不会返回WAIT_OBJECT_0也不会返回WAIT_OBJECT_0+1,则当出现该类情况时,则会一直运行while(1)循环,而造成该线程持续占用CPU而导致应用程序启动过慢或者根本无法启动</p>

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!