Russ Abbott wrote:
The WaitNeeded stuff sounds very interesting. I hadn't run across it before. I now see that there is some documentation in 10.1 of the base environment as Kari pointed out and in CTM 13.1.13. So far, I haven't been able to develop an intuition about it. Would you help me out with a simple example?

WaitNeeded is the primitive for building lazy computations. Upon creation, a variable X is simply free (unbound). When an operation requires the value of X to complete, the variable becomes "needed". It remains free, but is marked as "needed". This usually happens when a thread blocks on X.

{WaitNeeded X} simply blocks until X becomes needed. Being needed is monotonic, so WaitNeeded is not subject to race conditions. A lazy computation in Oz is simply a thread that waits until another thread needs its result.

Suppose I want to write some code that looked like this.
local Y
 proc {DontSuspend X} ... end
in
 {Browse Y+1}
end
and I wanted that code to throw an exception instead of suspending. What would I write in DontSuspend? (Raph's example seems to require that I put my {Browse X+1} code into DontSuspend, which is somewhat different--although that seemed to suspend instead of throwing an exception anyway.) As you can see, I need some help with this.

It depends *in which thread* you want to throw the exception... You can just let the DontSuspend thread browse a message like: "Someone suspends on X!". The other possibility is to bind X to a "failed value". A failed value is a special value that encapsulates an exception, and raises that exception in every thread attempting to use it. Failed values are convenient to report errors between concurrent computations. See the documentation of Value.failed.

proc {DontSuspend X}
   thread
      {WaitNeeded X}
      try %% succeeds only if X is still free
         X={Value.failed unexpectedSuspension(X)}
      catch _ then skip end
   end
end

Those concepts are explained in Chapter 4 of CTM.


Cheers,
raph

_________________________________________________________________________________
mozart-users mailing list                               
[email protected]
http://www.mozart-oz.org/mailman/listinfo/mozart-users

Reply via email to