首页
社区
课程
招聘
[求助]请问这是windows核心编程上的一个败笔,bug吗?
发表于: 2014-8-23 19:38 5002

[求助]请问这是windows核心编程上的一个败笔,bug吗?

2014-8-23 19:38
5002
核心编程第5版本  8.3节

代码:

volatile BOOL g_fFinishedCalculation = FALSE
主线程
{
创建子线程
while(!g_fFinishedCalculation);

}

子线程代码:
{
g_fFinishedCalculation = TRUE;
}

作者在 代码这里有一段文字:

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. If the thread executing the _tWinMain function is put to sleep instead of polling, it is not scheduled time and the system can schedule time to lower-priority threads, such as the RecalcFunc thread, allowing them to execute.

问题: 为什么优先级会导致 子线程 没有执行机会 ?

xp单核 系统上 做个实验, 发现 子线程是有机会执行的。

难道作者错了不成?

还有windows核心编程 上,提到过 动态提升线程优先级。

很明显,子线程会被动态提升到15.  自然会打破优先级逆转的问题。
所以子线程是有机会执行。

既然有机会执行,那么就说明 这段英文是有问题的。

[课程]Android-CTF解题方法汇总!

收藏
免费 0
支持
分享
最新回复 (10)
雪    币: 185
活跃值: (25)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
恭喜楼主。发现BUG了。 。。 楼主快找微软领奖去啊。
2014-8-23 20:01
0
雪    币: 54
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
冏,

作者不是微软的,

需要领奖,

想知道原因,
2014-8-23 20:05
0
雪    币: 2
能力值: (RANK:10 )
在线值:
发帖
回帖
粉丝
4

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.


注意是 可能.
2014-8-23 22:06
0
雪    币: 54
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
5
might后还有一句话:
This can happen if the primary thread has a higher priority than the thread executing the RecalcFunc function
2014-8-24 12:50
0
雪    币: 54
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
6
顶起来, 有高手吗?
2014-8-24 13:16
0
雪    币: 2
能力值: (RANK:10 )
在线值:
发帖
回帖
粉丝
7

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.


上面代码段中的问题是变量 xx 可能永远不会赋值成 true, 当主线程比执行 xx 的线程有更高的优先级时就会发生这种情况.
2014-8-24 13:45
0
雪    币: 1443
活跃值: (96)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
8
看起来是高优先级的线程一直都可以执行的话,低优先级的线程是得不到时间片的,但是windows考虑了这种情况,如果低优先级的线程出于就绪状态,但等待了很长时间(记得是4秒)都没得到执行机会,便会把它的优先级提升到15,它执行一个时间片之后就会再恢复老的优先级继续等待执行。
2014-8-24 14:51
0
雪    币: 54
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
9
3-4秒钟, 执行2个时间片, 后恢复到基本优先级。

所以这段英文是扯淡的
2014-8-24 23:00
0
雪    币: 155
活跃值: (20)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
10
Windows核心编程这种一知半解外围转转的书也有人奉为经典费劲找bug?

这种线程饥饿问题在windows internals里有介绍,学windows还是要找对windows internals这种比较靠谱的啊
2014-8-25 00:19
0
雪    币: 54
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
11
碉堡了,

不是号称圣经吗?
2014-8-25 10:46
0
游客
登录 | 注册 方可回帖
返回
//