Futures

The nature of the thread mechanism means that it lends itself quite naturally to providing a base for the implementation of a simple future abstraction. The acts of creating futures and of eventually interrogating them for their values map almost directly onto starting threads and accessing thread results.

The code for basic future manipulation is given below. A couple of examples of replacements for ``strict'' functions that allow for future objects are shown. The extensibility of generic functions and module renaming can be used to make these necessary changes transparent for users.

future

future expression*

Constructs a future object and spawns a thread to calculate the value of expression. An object of class future is returned by the expression resulting from the macro expansion. The implementation of future in is:

  (defmacro future exp
    `(let 
       ((future (make-future-object))
        (task (make-thread 
                (lambda (future fun)
                  ((setter future-object-value) future (fun))
                  ((setter future-object-done) future t)
                  t))))
         ((setter future-object-thread) future task)
         ((setter future-object-function) future (lambda () ,@exp))
	 (thread-start task future (lambda () ,@exp)) 
	 future))


futurepCompiler generic
\begin{arguments}
\item[obj] The object to be tested
\end{arguments}
nil if obj is not a future, otherwise, non-nil.


future-valueCompiler
\begin{arguments}
\item[future] The value to be evaluated
\end{arguments}
Forces the evaluation of a future and if the result of the evaluation is also a future that too is forced until the result is not a future.

future-selectCompiler
\begin{arguments}
\item[list] a list of futures
\end{arguments}
Return the first of future-list to complete