Skip to content

Instantly share code, notes, and snippets.

@mikbe
Created July 6, 2011 22:02
Show Gist options
  • Save mikbe/1068459 to your computer and use it in GitHub Desktop.
Save mikbe/1068459 to your computer and use it in GitHub Desktop.
Super call creates the mutex
require 'benchmark'
module SuperMutex
attr_reader :bar
def initialize
@mutex = Mutex.new
end
def threadsafe_mutex
@mutex
end
def some_method
threadsafe_mutex.synchronize {
@bar ||= 0
@bar += 1
}
end
end
class Foo
include SuperMutex
end
threads = []
thread_count = 700
Thread.abort_on_exception
Benchmark.bmbm(7) do |x|
x.report do
# it takes about half a second to create all of the threads so wait a full second to make sure they're all done being created
start = Time.now + 1.0
thread_count.times do
threads << Thread.new{sleep (start - Time.now); foo = Foo.new; 1000.times{foo.some_method} }
end
threads.each { |t| t.join }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment