[racket] understanding future/touch

From: Eric Tanter (etanter at dcc.uchile.cl)
Date: Tue Jan 25 11:03:22 EST 2011

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).

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))))

Posted on the users mailing list.