Skip to content

Instantly share code, notes, and snippets.

@benweint
Created August 29, 2013 05:19
Show Gist options
  • Select an option

  • Save benweint/6374519 to your computer and use it in GitHub Desktop.

Select an option

Save benweint/6374519 to your computer and use it in GitHub Desktop.
ActiveSupport::Notifications thread safety demo
#!/usr/bin/env ruby
require 'active_support'
class Frobnicator
def frobnicate
ActiveSupport::Notifications.instrument('frobnicate') do
puts 'frobnicating'
end
end
end
ActiveSupport::Notifications.subscribe('frobnicate') do |name, start, finish, id, payload|
raise "uh-oh, start was nil" if start == nil
end
nthreads = 16
iterations = 10000
threads = []
nthreads.times do |i|
threads << Thread.new do
Thread.current.abort_on_exception = true
frobnicator = Frobnicator.new
iterations.times do |j|
frobnicator.frobnicate
end
end
end
threads.each(&:join)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment