% Correct Version
proc {SubThread P}
%Notice that +1 is sent to the Port's stream before
%starting the new thread, this makes sure that sum of
%elements is *incremented before* any new thread is started
{Send Pt 1}
thread
{P} {Send Pt ~1}
end
end
% Incorrect Version
proc {SubThread P}
%thread is started before sending +1 to the port's stream
%so right after creation of this thread, if scheduler
%doesn't execute it then a thread is created but sum of
%elements is not incremented and that means assertion is
%no longer true.
thread
{Send Pt 1}
{P} {Send Pt ~1}
end
end
PartII- Required Example
declare NewThread
local
proc {ZeroExit N Is}
case Is of I|Ir then
if N+I\=0 then {ZeroExit N+I Ir}
else
% 'ZeroExit Finished' is displayed when call to
% ZeroExit is over.
{Browse 'ZeroExit Finished'}
end
end
end
in
proc {NewThread P ?SubThread}
Is Pt={NewPort Is}
in
proc {SubThread P}
thread
{Send Pt 1}
{P} {Send Pt ~1}
end
end
{SubThread P}
{ZeroExit 0 Is}
end
end
local SubThread in
{NewThread
proc {$}
{Browse 'T1 started'}
{SubThread
proc {$}
{Browse 'T2 started'} {Delay 2000}
{Browse 'T2 finished'}
end} %T2
{Browse 'T1 finished'}
end
SubThread}
{Browse 'NewThread Finished'}
end
Notice that "ZeroExit Finished" is printed before "T2 finished" that is sum of elemtents on the port stream became zero though T2 was not finished.
No comments:
Post a Comment