[plt-scheme] Parallel execution

From: Gregory Woodhouse (gregory.woodhouse at sbcglobal.net)
Date: Sat Jan 14 23:59:21 EST 2006

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

A test run produces output like this

x = 101
x = 121
x = 121
x = 101
...
x = 101
 >

This suggests to me that the threads are effectively atomic. The  
value of is never 11, 100 or 110. Now, my guess is that the running  
time of each of these threads is so small that they are never  
preempted before they complete. Is it at least theoretically possible  
that thread preemption could occur in such a way that t2 modifies x  
before t1 evaluates (* x x)? How might this be demonstrated using PLT  
Scheme? What does PLT stand for anyway?

===
Gregory Woodhouse
gregory.woodhouse at sbcglobal.net

"Before one gets the right answer, one must ask the right question."  
-- S. Barry Cooper





Posted on the users mailing list.