[racket] Future thread

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Mon Apr 18 16:35:34 EDT 2011

At Mon, 18 Apr 2011 13:44:05 -0600, Jay McCarthy wrote:
> I'd like the best of both worlds. I would imagine that this would work
> by having blocked futures be a schedule-able thread so that if they
> need to do unsafe work---and no other Racket thread is
> available---then they could be selected for a quanta before being put
> back in the truly parallel world.
> Do futures already work like this? 


> The documentation seems to suggest
> not. If not, is this possible?

This almost works:

 (define (my-future thunk)
   (let ([f (future thunk)])
     (thread (lambda () (touch f)))

If you're unlucky, however, the thread will start working on the future
before it's picked up for parallel execution. If parallel execution
gets stuck on an unsafe operation, the thread will take care of the
unsafe operation, and then... maybe the continuation gets picked up for
parallel execution again, or maybe you're unlucky at that later point
and the thread picks up the continuation.

A smarter Racket runtime might make it much less likely that you'll be
unlucky. I can imagine some heuristics toward that end. I can also
imagine some tweaks to future scheduling that could make `my-future'
reliable, but I'm less sure of the performance implications. Meanwhile,
I'm not sure how often you'll be unlucky with the current scheduler.

As Robby says, it's probably best to work from a real example.

Posted on the users mailing list.