Skip to content

Instantly share code, notes, and snippets.

@jackcallister
Created May 2, 2020 00:12
Show Gist options
  • Save jackcallister/dbb34003066a2506a888973908d42d69 to your computer and use it in GitHub Desktop.
Save jackcallister/dbb34003066a2506a888973908d42d69 to your computer and use it in GitHub Desktop.
# 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