Exercise 3.42. Ben Bitdiddle suggests that it's a waste of time to create a new serialized procedure in response to every
withdraw
anddeposit
message. He says thatmake-account
could be changed so that the calls to protected are done outside thedispatch
procedure. That is, an account would return the same serialized procedure (which was created at the same time as the account) each time it is asked for a withdrawal procedure.(define (make-account balance) (define (withdraw amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds")) (define (deposit amount) (set! balance (+ balance amount)) balance) (let ((protected (make-serializer))) (let ((protected-withdraw (protected withdraw)) (protected-deposit (protected deposit))) (define (dispatch m) (cond ((eq? m 'withdraw) protected-withdraw) ((eq? m 'deposit) protected-deposit) ((eq? m 'balance) balance) (else (error "Unknown request -- MAKE-ACCOUNT" m)))) dispatch)))Is this a safe change to make? In particular, is there any difference in what concurrency is allowed by these two versions of
make-account
?
Yes, it's safe. By definition, two serialized procedures will never be interleaved if both are created by the same serializer. The same can be said even if two serialized procedures are the same.
So that there is no difference between the two versions of make-account
.