10.4 Futures
Parallelism with Futures in The Racket Guide introduces futures.
Currently, parallel support for future is enabled by default for Windows, Linux x86/x86_64, and Mac OS X x86/x86_64. To enable support for other platforms, use --enable-futures with configure when building Racket.
The future and touch functions from racket/future provide access to parallelism as supported by the hardware and operating system. In contrast to thread, which provides concurrency for arbitrary computations without parallelism, future provides parallelism for limited computations. A future executes its work in parallel (assuming that support for parallelism is available) until it detects an attempt to perform an operation that is too complex for the system to run safely in parallel. Similarly, work in a future is suspended if it depends in some way on the current continuation, such as raising an exception. A suspended computation for a future is resumed when touch is applied to the future.
“Safe” parallel execution of a future means that all operations
provided by the system must be able to enforce contracts and produce
results as documented. “Safe” does not preclude concurrent access to
mutable data that is visible in the program. For example, a computation
in a future might use set! to modify a shared variable, in
which case concurrent assignment to the variable can be visible in other
futures and threads. Furthermore, guarantees about the visibility of
effects and ordering are determined by the operating system and
hardware—
A future never runs in parallel if all of the custodians that allow its creating thread to run are shut down. Such futures can execute through a call to touch, however.
Between a call to future and touch for a given future, the given thunk may run speculatively in parallel to other computations, as described above.
> (let ([f (future (lambda () (+ 1 2)))]) (list (+ 3 4) (touch f))) '(7 3)
procedure
procedure
(current-future) → (or/c #f future?)
procedure
(would-be-future thunk) → future?
thunk : (-> any)
With a normal future, certain circumstances might prevent the logging of unsafe operations. For example, when executed with debug-level logging,
(touch (future (lambda () (printf "hello1") (printf "hello2") (printf "hello3"))))
might log three messages, one for each printf invocation. However, if the touch is performed before the future has a chance to start running in parallel, the future thunk evaluates in the same manner as any ordinary thunk, and no unsafe operations are logged. Replacing future with would-be-future ensures the logging of all three calls to printf.
procedure
procedure
(make-fsemaphore init) → fsemaphore?
init : exact-nonnegative-integer?
A future semaphore is similar to a plain semaphore, but future-semaphore operations can be performed safely in parallel (to synchronize parallel computations). In contrast, operations on plain semaphores are not safe to perform in parallel, and they therefore prevent a computation from continuing in parallel.
procedure
(fsemaphore? v) → boolean?
v : any/c
procedure
(fsemaphore-post fsema) → void?
fsema : fsemaphore?
procedure
(fsemaphore-wait fsema) → void?
fsema : fsemaphore?
procedure
(fsemaphore-try-wait? fsema) → boolean?
fsema : fsemaphore?
procedure
(fsemaphore-count fsema) → exact-nonnegative-integer?
fsema : fsemaphore?