Ruby monitor segmentation fault


I followed the example from and modified the code a bit:

require 'monitor.rb'

buf = []
empty_cond = buf.new_cond

producer = Thread.start do
# producer
line = "produce at #{}"
#while line
  buf.synchronize do
    puts "==> #{line}"
  #line = "produce at #{}"

loop do
   buf.synchronize do
      empty_cond.wait_while { buf.empty? }
      item = buf.shift
      puts "got #{item.inspect}"

I let the program run. Around 5 min later, it throws a "Segmentation fault". Something related to a deadlock?


Best Solution

As your code stands (with the commented out while-statement in producer-loop) the producer thread simply runs through the loop once and exits. The consumer reads the one produced line from buf and then is left in a deadlock waiting for more lines that will never arrive.

Ruby's Thread scheduler has inbuilt deadlock-detection, so it will terminate the program when it sees that the 'consumer'-loop has deadlocked.

To see the deadlock for yourself, turn the producer into a global variable $producer and wrap the loop-statement with $consumer = Thread.start do ... end. Loading the code into irb and evaluating $producer should result in => #< Thread:0x000000010afb58 dead > (and $consumer in a sleeping thread)

Take out the comments relating to producer's while-loop and you'll have a working (infinite) loop that produces the current time at 2 second intervals.