mootoh (owner)

Revisions

gist: 166921 Download_button fork
public
Public Clone URL: git://gist.github.com/166921.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
#
class Account
  def initialize
    @balance = 0
    @mutex = Mutex.new
  end
 
  def withdraw(n)
    @mutex.synchronize {
      @balance = @balance - n
    }
  end
 
  def deposit(n)
    withdraw(-n)
  end
end
 
def transfer(from, to, amount)
   from.lock; to.lock
   from.withdraw(amount)
   to.deposit(amount)
   from.unlock; to.unlock
end