Project

General

Profile

Backport #4009 » proof_of_segfault.rb

program that segfaults - nanodeath (Max Aller), 11/01/2010 02:43 AM

 
require "thread"
require "monitor"

joblist = []
done_count = 0
class Worker < Thread
def initialize(joblist, mon, input_cv, output_cv)
super do
while true
mon.synchronize do
input_cv.wait_until { !joblist.empty? }
job = joblist.shift
end

# do some job processing here....
mon.synchronize do
done_count += 1
# this line is never reached because the previous line actually raises an error
# since done_count isn't in scope
output_cv.broadcast
end
end
end
end
end

mon = Object.new
mon.extend MonitorMixin
input_cv = mon.new_cond
output_cv = mon.new_cond

1.upto(100) {|i| joblist << i}

# The "delayed adder" thread
Thread.new do
10.times do |i|
mon.synchronize do
joblist << (i + 2) * 100
input_cv.signal
end
sleep 1
end
end

workers = []
100.times { workers << Worker.new(joblist, mon, input_cv, output_cv) }
mon.synchronize do
output_cv.wait_until { joblist.empty? }
end
(1-1/3)