-
-
Save eregon/ab7c9a89da614139105da414247dafaf to your computer and use it in GitHub Desktop.
This file contains 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
def thread_pool | |
N_THREADS.times.map { |t| | |
q = Queue.new | |
ret = Queue.new | |
Thread.new { | |
ret.push :started | |
while job = q.pop | |
ret.push thread_bench(t) | |
end | |
} | |
[q, ret] | |
} | |
end | |
def thread_bench(t) | |
Thread.pass until $go # Commenting this avoids the problem | |
p [:running, t] | |
ops = 0 | |
while $run | |
Thread.pass | |
ops += 1 | |
end | |
ops | |
end | |
def measure_ops | |
threads = thread_pool | |
# Wait for all threads to start | |
threads.each { |q,ret| ret.pop } | |
20.times do | |
puts | |
$go = false | |
$run = true | |
threads.each { |q,ret| q.push :token } | |
$go = true | |
p [:released, "$go=#{$go}"] | |
sleep 1 | |
$run = false | |
threads.map { |q,ret| ret.pop } | |
end | |
threads.each { |q,ret| q.push nil } | |
threads.each(&:join) | |
:done | |
end | |
trap(:USR2) { | |
Thread.list.each { |th| | |
p th | |
puts th.backtrace | |
puts | |
} | |
} | |
N_THREADS = Integer(ARGV[0]) | |
p measure_ops |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment