我按照http://www.ruby-doc.org/stdlib/libdoc/monitor/rdoc/index.html中的示例对代码进行了一些修改:
require 'monitor.rb'
buf = []
buf.extend(MonitorMixin)
empty_cond = buf.new_cond
producer = Thread.start do
# producer
line = "produce at #{Time.now}"
#while line
buf.synchronize do
puts "==> #{line}"
buf.push(line)
empty_cond.signal
end
sleep(2)
#line = "produce at #{Time.now}"
#end
end
loop do
buf.synchronize do
empty_cond.wait_while { buf.empty? }
item = buf.shift
puts "got #{item.inspect}"
end
end
我让程序运行。大约 5 分钟后,它会抛出“分段错误”。与死锁有关的东西?
/杰克