mootoh (owner)

Revisions

gist: 166919 Download_button fork
public
Public Clone URL: git://gist.github.com/166919.git
Embed All Files: show embed
account.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# from Beautiful Code Sec 24, p.406
#
require 'stm'
 
class Account
  def initialize
    @tvar = STM::TVar.new(0)
  end
 
  def withdraw(amount)
    balance = @tvar.read
    @tvar.write(balance - amount)
  end
 
  def deposit(amount)
    withdraw(amount)
  end
end
 
def transfer(from, to, amount)
   STM.atomically {
      from.withdraw(amount)
      to.deposit(amount)
   }
end