理系学生日記

おまえはいつまで学生気分なのか

問題3-44 (3.4.1 The Nature of Time in Concurrent Systems)

2 つの口座の残高を交換する exchange は,直列化しとかないとヒドいことになる.

(define (exchange account1 account2)
  (let ((difference (- (account1 'balance)
                       (account2 'balance))))
    ((account1 'withdraw) difference)
    ((account2 'deposit) difference)))

では,一定金額 amount をある口座から他方の口座へ移動させる transfer を直列化する必要はあるのかという話.

(define (transfer from-account to-account amount)
  ((from-account 'withdraw) amount)
  ((to-account 'deposit) amount))

exchange を直列化する必要があったのは,計算した 2 口座 (account1 と account2) 間の残高の差が,withdraw と deposit を完了するまで一定に保たれていなければならないから.この間にどちらかの口座の残高が変更された場合,この処理は破綻する.

ところが transfer は,あらかじめ処理する金額が決まっている.withdraw と deposit の間であれ,前後であれ,口座の残高が変わったとしても一貫性は保たれる.そのため,直列化する必要はない.んじゃないかな.