1 day ago
my attempt to do the exercises in sicp.
Thursday, March 18, 2010
sicp exercise 3.39
;; Exercise 3.39. Which of the five possibilities in the parallel execution shown above remain if we instead serialize execution as follows:
;;
;; (define x 10)
;;
;; (define s (make-serializer))
;;
;; (parallel-execute (lambda () (set! x ((s (lambda () (* x x))))))
;; (s (lambda () (set! x (+ x 1)))))
;; Required for (parallel) on Guile
(use-modules (ice-9 threads))
;; Following is the version of (make-serializer) on Guile
(define (make-serializer)
(let ((mutex (make-mutex)))
(lambda (p)
(define (serialized-p . args)
(lock-mutex mutex)
(let ((val (apply p args)))
(unlock-mutex mutex)
val))
serialized-p)))
(define x 10)
(define s (make-serializer))
(parallel ((lambda () (set! x ((s (lambda () (* x x)))))))
((s (lambda () (set! x (+ x 1))))))
(display x) (newline)
;; Evaluates to 101
;; But if the order is swapped, the output is changed
(set! x 10)
(parallel ((s (lambda () (set! x (+ x 1)))))
((lambda () (set! x ((s (lambda () (* x x))))))))
(display x) (newline)
;; Evaluates to 121
;; Ans:
;; theoretical possibiities are : 121, 100, 101
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment