Skip to content
Browse files

ch3

  • Loading branch information...
1 parent 399c3c4 commit 2ffec6bdc0a5e0a7d2bf599b37a1c076dd9bff53 @flaming0 committed
Showing with 44 additions and 3 deletions.
  1. +6 −0 ch3/ex3-39.scm
  2. +38 −3 ch3/test_concurrency.scm
View
6 ch3/ex3-39.scm
@@ -0,0 +1,6 @@
+;; Example 3.39
+
+(define x 10)
+(define s (make-serializer))
+(parallel-execute (lambda () (set! x ((s (lambda () (* x x))))))
+ (s (lambda () (set! x (+ x 1)))))
View
41 ch3/test_concurrency.scm
@@ -2,9 +2,44 @@
;; http://planet.racket-lang.org/display.ss?package=sicp-concurrency.plt&owner=dyoo
;; install package, reboot drracket, change language to "SICP Concurrency language"
+;; 121 101 11 100 110
+
+(define (test)
+ (define (iter count)
+ (define x 10)
+ (if (<= count 0)
+ #t
+ (begin (parallel-execute (lambda () (set! x (* x x)))
+ (lambda () (set! x (+ x 1))))
+ (display x)
+ (newline)
+ (iter (- count 1)))))
+ (iter 40))
+
+(test)
+
(define x 10)
+(define s (make-serializer))
+(parallel-execute (s (lambda () (set! x (* x x))))
+ (s (lambda () (set! x (+ x 1)))))
-(parallel-execute (lambda () (set! x (* x x)))
- (lambda () (set! x (+ x 1))))
+x
-x
+(define (make-account balance)
+ (define (withdraw amount)
+ (if (>= balance amount)
+ (begin (set! balance (- balance amount))
+ balance)
+ "Insufficient funds\n"))
+
+ (define (deposit amount)
+ (set! balance (+ balance amount))
+ balance)
+
+ (let ((protected (make-serializer)))
+ (define (dispatch m)
+ (cond ((eq? m 'withdraw) (protected withdraw))
+ ((eq? m 'deposit) (protected deposit))
+ ((eq? m 'balance) balance)
+ (else (error "Unknown request" m))))
+ dispatch))

0 comments on commit 2ffec6b

Please sign in to comment.
Something went wrong with that request. Please try again.