Exercise 3.38: Suppose that Peter, Paul, and
Mary share a joint bank account that initially contains $100. Concurrently,
Peter deposits 20, and Mary withdraws half the money in
the account, by executing the following commands:
Peter: (set! balance (+ balance 10))
Paul: (set! balance (- balance 20))
Mary: (set! balance (- balance
(/ balance 2)))
List all the different possible values for balance after these three
transactions have been completed, assuming that the banking system forces the
three processes to run sequentially in some order.
What are some other values that could be produced if the system allows the
processes to be interleaved? Draw timing diagrams like the one in Figure 3.29
to explain how these values can occur.
练习 3.38:假设 Peter、Paul 和 Mary 共享一个最初存有 10,Paul 取出 $20,Mary 取出账户中一半的钱:
Peter: (set! balance (+ balance 10))
Paul: (set! balance (- balance 20))
Mary: (set! balance (- balance
(/ balance 2)))
假设银行系统强制三个计算过程按某种顺序依次运行,列出这三笔交易完成后 balance 所有可能的值。
如果系统允许这些计算过程交错执行,还可能产生哪些其他值?请画出类似图 3.29 的时序图,说明这些值是如何产生的。
Peter: (set! balance (+ balance 10))
Paul: (set! balance (- balance 20))
Mary: (set! balance (- balance
(/ balance 2)))