Skip to content

Instantly share code, notes, and snippets.

@eregon
Created September 25, 2017 13:30
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 eregon/ab7c9a89da614139105da414247dafaf to your computer and use it in GitHub Desktop.
Save eregon/ab7c9a89da614139105da414247dafaf to your computer and use it in GitHub Desktop.
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