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

关于《windows via c&C++》中线程的疑问,感觉作者写的有有关问题

2012-03-02 
关于《windows via c&C++》中线程的疑问,感觉作者写的有问题。C/C++ codevolatile BOOL g_fFinishedCalculati

关于《windows via c&C++》中线程的疑问,感觉作者写的有问题。

C/C++ code
volatile BOOL g_fFinishedCalculation = FALSE;  int WINAPI _tWinMain(...) {     CreateThread(..., RecalcFunc, ...);     ...     // Wait for the recalculation to complete.    while (!g_fFinishedCalculation)          ;     ... } DWORD WINAPI RecalcFunc(PVOID pvParam) {     // Perform the recalculation.     ...     g_fFinishedCalculation = TRUE;     return(0); } 


Another problem with the polling method used in the previous code fragment is that the BOOL variable 
g_fFinishedCalculation might never be set to TRUE. This can happen if the primary thread has a higher priority than the thread executing the RecalcFunc function. In this case, the system never assigns any time slices to the RecalcFunc thread, which never executes the statement that sets g_fFinishedCalculation to TRUE.

作者说可能RecalcFunc得不到执行,我感觉不会啊。这个线程如果starve的话,系统会自动提高它的优先级啊。

[解决办法]
只是提出一种假设。
windows下饥饿的确会引起线程优先级的动态提升。但是这种提升再怎么生也只是15以下,万一主线程优先级在16以上呢

热点排行