Skip to content

Instantly share code, notes, and snippets.

@japgolly
Created April 22, 2012 23:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save japgolly/2467503 to your computer and use it in GitHub Desktop.
Save japgolly/2467503 to your computer and use it in GitHub Desktop.
require 'benchmark'
require 'thread'
require 'monitor'
mutex = Mutex.new
monitor = Monitor.new
nop = proc{}
range = 1..1_000_000
for j in 1..20
puts "Warmup ##{j}/20"
for i in range; mutex.synchronize &nop end
for i in range; monitor.synchronize &nop end
end
puts
Benchmark.bm(10) do |x|
x.report('Mutex') { for i in range; mutex.synchronize &nop end }
x.report('Monitor'){ for i in range; monitor.synchronize &nop end }
end
require 'benchmark'
require 'thread'
require 'monitor'
mutex = Mutex.new
monitor = Monitor.new
nop = proc{}
range = 1..1_000_000
Benchmark.bm(10) do |x|
x.report('Mutex') { for i in range; mutex.synchronize &nop end }
x.report('Monitor'){ for i in range; monitor.synchronize &nop end }
end
require 'monitor'
m= Monitor.new
m.synchronize {
m.synchronize {
puts "Monitor is reentrant."
}
}
require 'thread'
m= Mutex.new
m.synchronize {
m.synchronize {
puts "Mutex is reentrant."
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment