Race condition in Search.ozf
Luis Quesada
luque at info.ucl.ac.be
Thu Feb 2 17:50:52 CET 2006
Harmon Nine wrote:
>So the program would look something like this?
>
>==========================
>Sobj = {New Search.object script(Space OptProc)}
>
>proc {WillContinue NextSolution} {SObj next(NextSolution)} end
>proc {WillStop NextSolution} NextSolution = stopped end
>
>P = {NewCell WillContinue}
>
>Thread 1:
>proc {GetNextSolution}
> NextSolution
> in
> {@P NextSolution}
>
> if NextSolution == stopped then
> skip
> else
> Process NextSolution
> {GetNextSolution}
> end
>
>end
>
>Thread 2:
>proc {StopThread}
> P := WillStop
> {Sobj stop}
>end
>==========================
>
>
>
What I mean is to redifine the Order that is passed as a paramenter to
the script method:
P={NewCell "your order procedure"}
Order= proc{$ Old New} {@P Old New} end
Sobj={New Search.object script(Script Order)}
Thread 1:
proc {GetNextSolution}
NextSolution
in
{Sobj next(NextSolution)}
if {Member NextSolution [stopped nil]} then
skip
else
Process NextSolution
{GetNextSolution}
end
end
Thread 2:
proc {Stop}
P:=proc {$ _ _} fail end
{Sobj stop}
end
>This is a better solution, i.e. it is deterministic in that the call in
>Thread 2 to the StopThread proc is guaranteed to (eventually) stop
>calculation. However there's still a race that has a (very small)
>chance of causing the calculations not to stop immediately when the
>"stop" method is called on SObj. An immediate termination would be
>necessary on receipt of a user input event, or the expiration of a
>soft-real-time timer.
>
>Scenario:
>@P has value WillContinue
>In Thread 1 (top of GetNextSolution proc), @P is dereferenced and thus
>WillContinue is called.
>Program execution in Thread 1 is now in the WillContinue proc, but
>Thread 1 is pre-empted *before* the {SObj next(NextSolution}} call.
>
>Thread 2 fires up on user input event, changes the value of P to
>WillStop and calls the stop method on SObj.
>This call to the stop method sets the 'isStopped' flag for the Search
>object SObj.
>Thread 2 is pre-empted.
>
>
If Stop has been executed before {Sobj next(NextSolution)},
{Sobj next(NextSolution)} will return immediately because of the failure exception.
Luis
More information about the mozart-hackers
mailing list