[plt-scheme] Parallel execution

From: Robby Findler (robby at cs.uchicago.edu)
Date: Sat Jan 14 18:28:36 EST 2006

The thread switching granularity is larger than you seem to think, but
the two threads are indeed independent. Try something like this:

(define (parallel-execute . args) (for-each thread args))

;; f : display-able -> (-> alpha)
;; returns a thunk that runs an infinite loop, displaying x
(define (f x) (lambda () (display x) ((f x))))

(parallel-execute (f 'a) (f 'b))

to see interleaving.

Robby

At Sat, 14 Jan 2006 15:19:30 -0800, Gregory Woodhouse wrote:
> I've been working my way through SICP and am currently in chapter 3.  
> Right now, I'm reading about parallel execution. The syntax they use is
> 
> (parallel-execute <thunk> <thunk> ...)
> 
> I'm not sure how to do the same thing in PLT Scheme, but I did try
> 
> (begin (thread (lambda () (display "A"))) (thread (lambda () (display  
> "B"))))
> 
> and sometimes get "AB" and sometimes "BA" (as I had hoped). So, how  
> do begin and thread interact? I had expected that I'd see "AB" much  
> more often than "BA" because I thought each thread would be scheduled  
> as soon a (thread <thunk>) was evaluated. But now, I wonder how  
> thread is implemented, and if none of the threads would actually run  
> until the begin block was evaluated. Then I began to wonder how if  
> there was just one "engine" (for lack of a better term) carrying out  
> a single reduction in each thread and then moving on to another  
> (round robin scheduling?)
> 
> Just wondering.
> 
> ===
> Gregory Woodhouse
> gregory.woodhouse at sbcglobal.net
> 
> "The whole of science is nothing more than a refinement
>   of everyday thinking."  -- Albert Einstein
> 
> 
> 
> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme


Posted on the users mailing list.