3.40 Exercise 3.40
(define x 10) (parallel-execute (lambda () (set! x (* x x))) (lambda () (set! x (* x x x))))
The different outcomes will occur because of the different places that the value of x can be changed, depending on which procedure finishes first and at what point it does. The following can happen:
1000000: The first procedure finishes first, resulting in (* 100 100 100)
100000: The first procedure finishes after one read in the second, resulting in (* 10 100 100)
10000: The first procedure finishes after two reads in the second, resulting in (* 10 10 100)
1000: The first procedure sets x after the second procedure calculates the new value of x
1000000: The second procedure finishes first, resulting in (* 1000 1000) (note that this is the same as the first case)
10000: The second procedure finishes after one read in the first, resulting in (* 10 1000)
100: The second procedure sets x after the first procedure calculates the new value of x
Now suppose we use serialized procedures as such:
(define x 10) (define s (make-serializer)) (parallel-execute (s (lambda () (set! x (* x x)))) (s (lambda () (set! x (* x x x)))))
In this case, the only possible answer is 1000000, because the order in which we perform the multiplications does not matter as long as they all contribute to the answer. It doesn’t matter which procedure runs first – the answer will be the same.