[racket] understanding future/touch

From: Jay McCarthy (jay.mccarthy at gmail.com)
Date: Tue Jan 25 11:17:46 EST 2011

2011/1/25 Eric Tanter <etanter at dcc.uchile.cl>:
> Hi,
>
> I'd like to understand a point about futures.
>
> The doc says:
> "Between a call to future and touch for a given future, the given thunk may run speculatively in parallel to other computations"
>
> (noticing the _may_)
>
> In order to know if it does, and when, I added a write in the future computation.
>
> (let ([f (future (lambda () (write "ok") (any-double? l2)))])
>        (list (any-double? l1)
>              (touch f)))
>
> I am surprised that if I don't call touch, the future computation is never run (at least not in the many many tries I did).

How are you testing that it is not run? If the future needs to do
something to the global VM state, then it will block and wait to
resume on the touch, but everything before that will still be done in
parallel.

Jay

>
> and if I call touch (even in another function [*]), I get the 2x speedup.
>
> So how do you magically determine whether or not to run the thunk in parallel?
>
> Thanks
>
> -- Éric
>
> [*]
>
> (define (foo x)
>  (touch x))
>
> (time (let ([f (future (lambda () (any-double? l2)))])
>        (list (any-double? l1)
>              (foo f))))
> _________________________________________________
>  For list-related administrative tasks:
>  http://lists.racket-lang.org/listinfo/users
>



-- 
Jay McCarthy <jay at cs.byu.edu>
Assistant Professor / Brigham Young University
http://faculty.cs.byu.edu/~jay

"The glory of God is Intelligence" - D&C 93


Posted on the users mailing list.