Created
July 6, 2011 22:02
-
-
Save mikbe/1068459 to your computer and use it in GitHub Desktop.
Super call creates the mutex
This file contains hidden or 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
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