读书人

关于《windows via camp;C++》中线程的疑

发布时间: 2012-03-02 14:40:28 作者: rapoo

关于《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以上呢

读书人网 >VC/MFC

热点推荐