Skip to content

Instantly share code, notes, and snippets.

@jstotz
Created December 8, 2010 17:20
Show Gist options
  • Save jstotz/733582 to your computer and use it in GitHub Desktop.
Save jstotz/733582 to your computer and use it in GitHub Desktop.
success_count = 0
fail_count = 0
lock = Mutex.new
DB = Application.db
DB.drop_table :race_test rescue nil
DB.create_table :race_test do
primary_key :id
Integer :value
end
1_000.times do
id = DB[:race_test].insert({:value => 0})
thread1 = Thread.new do
DB.transaction do
DB[:race_test].filter(:id => id).update({:value => :value + 1})
end
end
thread2 = Thread.new do
DB.transaction do
DB[:race_test].filter(:id => id).update({:value => :value + 10})
end
end
thread1.join
thread2.join
if DB[:race_test][:id => id][:value] == 11
lock.synchronize { success_count += 1 }
else
lock.synchronize { fail_count += 1 }
end
end
puts "Failed: #{fail_count}, OK: #{success_count}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment