Sunday, February 1, 2009

Ch4, Ex6

fun {Sum Xs A}
case {Skip1 Xs}
of X|Xr then {Sum Xr A+X}
[] nil then A
end
end
local Xs S in
thread Xs={Generate 0 150000} end %T1
thread S={Sum Xs 0} end %T2
{Browse S}
end

Let us say thread scheduler does the following schedule:

T1 is executed some time and Xs=0|1|2|3|4|_ at the time of its preemption.
T2 is exexuted and Skip1 skips 0,1,2,3... which effectively means they are lost and not included in the sum. This goes on and we keep on losing everything but last generated element in Xs everytime T2 is executed again after preemption of T1 and that results in calculated value less than actual value.

No comments:

Post a Comment