[racket] Understanding future syncronization

From: Robby Findler (robby at eecs.northwestern.edu)
Date: Mon Dec 31 23:13:38 EST 2012

Apparently nullary void doesn't have a barricade, so you can write
something like

  (begin (box-cas! ...) (void))

instead of what you have.

I'm not sure why the unary version is barricaded; my guess is that this is
something straightforward to fix, but at least there is a workaround in the
meantime.

Robby




On Mon, Dec 31, 2012 at 9:41 PM, Eric Dobson <eric.n.dobson at gmail.com>wrote:

> I am playing around with futures currently, and am trying to understand
> how to make my programs faster. The future visualizer is wonderful in
> understanding whats going on, but it lead me down a track which made my
> program slower.
>
> The issue is that void primitive needs to synchronize with the main
> thread. So when I was using it to provide the return value from my function
> it was synchronizing. I thought that not calling a function and just
> referencing a module-level constant would solve this synchronization issue.
> And it did, but it made the program slower.
>
> What is the rule of thumb for understanding how expensive synchronization
> is, I assumed it was at least the cost of a couple atomic operations? Also
> can someone explain why void needs to synchronize with the main thread?
>
>
> My (reduced) program:
> #lang racket
>
> (define void-t (void))
> #;
> (define-syntax-rule (my-void arg)
>   (begin
>     arg
>     void-t))
> (define my-void void)
>
>
> (define (run)
>   (define N 1000000)
>   (define v (box #f))
>   (define (f)
>     (future
>       (thunk
>         (for ((i (in-range N)))
>           (my-void (box-cas! v (unbox v) i))))))
>   (define f1 (f))
>   (define f2 (f))
>   (touch f1)
>   (touch f2)
>   (values))
>
>
>
> (define (time-it)
>   (collect-garbage)
>   (time (run))
>   (collect-garbage)
>   (time (run))
>   (collect-garbage)
>   (time (run))
>   (collect-garbage)
>   (time (run))
>   (collect-garbage)
>   (time (run))
>   (collect-garbage)
>   (time (run))
>   (collect-garbage)
>   (time (run))
>   (collect-garbage)
>   (time (run))
>   (collect-garbage)
>   (time (run))
>   (collect-garbage)
>   (time (run))
>   (collect-garbage)
>   (time (run)))
>
> #|
> (time-it)
> |#
>
>
> (require future-visualizer)
> (visualize-futures
>   (run)
>   #f)
>
>
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20121231/39319a26/attachment.html>

Posted on the users mailing list.