Skip to content

Instantly share code, notes, and snippets.

@ryoqun
Last active December 15, 2015 05:39
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 ryoqun/5210320 to your computer and use it in GitHub Desktop.
Save ryoqun/5210320 to your computer and use it in GitHub Desktop.
module M
class A
def get_value
end
end
class B
def get_value
end
end
end
def run(&hot_loop_block)
if ENV["PARALLEL"]
puts "multi-threaded"
Thread.new(&hot_loop_block)
elsif ENV["LOCK"]
puts "multi-threaded (using lock)"
@lock = Mutex.new
Thread.new(&hot_loop_block)
else
puts "single-threaded"
hot_loop_block.call
nil
end
end
LOOP_COUNT = 500000
def iterate(&iteration_block)
if @lock
LOOP_COUNT.times do
@lock.synchronize(&iteration_block)
end
else
LOOP_COUNT.times(&iteration_block)
end
end
module N
puts(RUBY_DESCRIPTION)
definition_body = lambda do
proc do
def get_value
nil
end
end.call
end
thread_a = run do
iterate do
M::A.class_exec { undef :get_value }
M::A.class_exec(&definition_body)
M::A.new.get_value
end
end
thread_b = run do
iterate do
M::B.class_exec { undef :get_value }
M::B.class_exec(&definition_body)
M::B.new.get_value
end
end
thread_a.join if thread_a
thread_b.join if thread_b
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment