【SICP练习】115 练习3.41
2015-09-08 00:00
417 查看
练习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
版权声明:本文为 NoMasp柯于旺 原创文章,未经许可严禁转载!欢迎访问我的博客:http://blog.csdn.net/nomasp
相关文章推荐
- 社交巨头三国杀:微信、WhatsApp、Line到底有啥区别?
- php中define用法实例
- PHP define函数的使用说明
- typedef和#define的用法以及区别
- PHP中使用hidef扩展代替define提高性能
- 探讨php define()函数及defined()函数使用详解
- 大连圈线下活动总结
- 负载均衡及会话保存(Ofbiz框架)
- 条款02:尽量以const,enum,inline替换#define
- 重新配置会计科目表
- haproxy负载均衡保持客户端和服务器Session亲缘性的三种方式:
- 社会工程学分析软件maltego的简单介绍
- load balance (nat)
- php中const与define的使用区别
- automatic send emal to user when create account
- PostgreSQL存储过程返回数据集实例
- 绘制直线
- nodejs windows 环境变量配置
- Linux 环境下Oracle安装与调试(二)之创建数据库,数据增删改
- const 和 #define 的区别