(lispkit-thread-future)
Last updated
Last updated
Library (lispkit thread future)
implements futures, an abstraction allowing for fine-grained concurrent computation of values. A future encapsulates an expression whose computation may occur in parallel with the code of the calling thread and all other threads that are currently executed including other futures. Like promises, futures are proxies that can be queried to obtain the value of the encapsulated computation. While promises compute values on demand, futures compute values in parallel.
Futures are created either with special form future
or procedure make-future
. The creation of a future implicitly kicks off the parallel computation of the future's value. The value can be accessed via future-get
, which will block if the computation of the value has not finished yet. future-get
can optionally be given a timeout to control the maximum number of seconds a thread is waiting for the results of a future to become available. Via procedure future-done?
it is possible to check in a non-blocking manner if the future has finished computing its value already.
future-type-tag
Symbol representing the future
type. The type-for
procedure of library (lispkit type)
returns this symbol for all future objects.
(future? obj)
Returns #t
if obj is a future object; #f
otherwise.
(future expr)
Via the future
syntax it is possible to create a future which evaluates expr in parallel in the same environment in which future
appears. The future
syntax returns a future object which eventually will contain the result of evaluating expr concurrently. If an exception is raised within expr, it is not caught but delivered later when future-get
gets invoked.
(make-future proc)
Returns a new future which computes its value by executing thunk proc in parallel in the same environment in which make-future
is called. If an exception is raised within expr, it is not caught but delivered later when future-get
gets invoked.
(make-evaluated-future obj)
Returns a new future which encapsulates obj as its value without doing any computation. When future-get
gets invoked, obj is returned.
(make-failing-future exc)
Returns a new future which encapsulates exception exc as its value without doing any computation. When future-get
gets invoked, exc is being raised.
(future-done? f)
Returns #t
if the computation of future f is finished and the value of f available; otherwise #f
is returned. This procedure does not block.
(future-get f) (future-get f timeout) (future-get f timeout default)
Returns the value associated with future f. If the value of f is not available yet (because the computation is still ongoing), this procedure will block. timeout defines the maximum number of seconds future-get
is waiting for receiving the value of f. If timeout is not provided, future-get
will wait indefinitely. When future-get
times out, default is returned as its result. If default is not provided, then future-get
will raise an error when timing out. If an exception is raised during the computation, future-get
will raise this exception every single time it is invoked.
(touch f)
is equivalent to (future-get f)
, but does not support the optional parameters of future-get
. It is provided for compatibility with future implementations of other Scheme implementations.
(touch f)