Created
May 2, 2020 00:12
-
-
Save jackcallister/dbb34003066a2506a888973908d42d69 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example of concurrency issue: | |
class Account | |
attr_accessor :amount | |
def initialize | |
@amount = 0 | |
end | |
def get_balance | |
@amount | |
end | |
def update_balance val | |
return false if @amount.nil? | |
@amount = @amount + val | |
end | |
def close_account | |
@amount = nil | |
end | |
end | |
account = Account.new() | |
puts account.get_balance | |
5.times do |i| | |
Thread.new do | |
account.update_balance 10 | |
end | |
end | |
puts account.get_balance | |
# output 30 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment