[plt-scheme] Parallel execution
On Sat, 14 Jan 2006, Gregory Woodhouse wrote:
> Okay, one more :-)
>
> Consider the following code
>
> (define t1 '())
> (define t2 '())
> (let loop ((i 0))
> (begin
> (if (< i 25)
> (begin
> (set! x 10)
> (set! t1 (thread
> (lambda () (set! x (* x x)))))
> (set! t2 (thread
> (lambda () (set! x (+ x 1)))))
> (begin
> (sleep 2)
> (display "x = ")
> (display x)
> (newline))
> (loop (+ i 1))))))
Hi Gregory,
Let's try something:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Sleeps for a random period of time (up to a second), then returns x
(define (identity-with-sleep x)
(sleep (random))
x)
(define (test-threading)
(let ((x 10))
(thread (lambda () (set! x (* (identity-with-sleep x)
(identity-with-sleep x)))))
(thread (lambda () (set! x (+ (identity-with-sleep x)
(identity-with-sleep 1)))))
(sleep 2)
(printf "x = ~s~%" x)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Here's what I see with a few runs:
;;;;;;
> (test-threading)
x = 11
> (test-threading)
x = 11
> (test-threading)
x = 11
> (test-threading)
x = 11
> (test-threading)
x = 110
> (test-threading)
x = 100
> (test-threading)
x = 11
;;;;;;
So this does show that threads can be preempted. I think your earlier
experiments just need the threads to do enough work to trigger preemption.
I do remember seeing something out of PLT Redex that might be appropriate
to your question; check "A Visual Environment for Developing
Context-Sensitive Term Rewriting Systems":
http://www.ccs.neu.edu/scheme/pubs/rta2004-mfff.pdf
The part in section 3.3 in that paper talks about getting PLT Redex to
show all the possibilities from concurrency.
> What does PLT stand for anyway?
PLT -- Programming Language Theory. See:
http://www.plt-scheme.org/who/
Best of wishes!