# (lispkit-thread-future)

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** <img src="/files/viHiVHsCY8Wn2TeCgWJj" alt="" data-size="line">

Symbol representing the `future` type. The `type-for` procedure of library `(lispkit type)` returns this symbol for all future objects.

**(future?&#x20;*****obj*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Returns `#t` if *obj* is a future object; `#f` otherwise.

**(future&#x20;*****expr*****)** <img src="/files/gEcsZuRGyhWFw4tM60U7" alt="" data-size="line">

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&#x20;*****proc*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

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&#x20;*****obj*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

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&#x20;*****exc*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

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?&#x20;*****f*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

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&#x20;*****f*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">\
\&#xNAN;**(future-get&#x20;*****f timeout*****)**\
\&#xNAN;**(future-get&#x20;*****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&#x20;*****f*****)**     <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

`(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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.lisppad.app/libraries/lispkit/lispkit-thread-future.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
