【SICP練習】115 練習3.41,sicp練習1153.41

來源:互聯網
上載者:User

【SICP練習】115 練習3.41,sicp練習1153.41
練習3-41原文

Exercise 3.41. Ben Bitdiddle worries that it would be better to implement the bank account as follows (where the commented line has been changed):

(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)       ;; continued on next page       (let ((protected (make-serializer)))           (define (dispatch m)                 (cond ((eq? m 'withdraw) (protected withdraw))                                                  ((eq? m 'deposit) (protected deposit))                                                ((eq? m 'balance)                             ((protected (lambda () balance)))) ; serialized                                              (else (error "Unknown request -- MAKE-ACCOUNT"                                                           m))))      dispatch))

because allowing unserialized access to the bank balance can result in anomalous behavior. Do you agree?
Is there any scenario that demonstrates Ben’s concern?

分析

假設有((protected withdraw) 100)和((protected deposit) 50)兩個進程分別和balance並存執行。則會有4種可能的執行順序。
withdraw - > balance 操作時,首先將餘額設定為0,然後返回balance得到0。
balance - > withdraw操作時,首先balance會得到100,然後執行withdraw操作得到0。
deposit - > balance操作時,首先將餘額設定為150,然後返回balance得到150。
balance - > deposit操作時,首先balance會得到100,然後deposit操作得到150。
由此可見Ben並沒有擔心的必要。

為使本文得到斧正和提問,轉載請註明出處:
http://blog.csdn.net/nomasp

郵箱及Skype:nomasp@outlook.com
Facebook:https://www.facebook.com/yuwang.ke
CSDN部落格:http://blog.csdn.net/nomasp
新浪微博:http://weibo.com/nomasp

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.