#628 open
rosejn

[Bug] Queue is broken on Mutex#sleep

Reported by rosejn | July 8th, 2008 @ 06:07 AM | in 1.0 preview

The following Queue example code fails because in Queue#pop on line 284 of lib/thread.rb, @mutex.sleep is called. I would have thought this should be Thread.stop, as it is in MRI, or just Kernel#sleep, but on a first shot neither worked. Anyone know the correct way to sleep here?

------------------------------------------------------

require 'thread'

queue = Queue.new

producer = Thread.new do
  5.times do |i|
    sleep rand(i) # simulate expense
    queue << i
    puts "#{i} produced"
  end
end

consumer = Thread.new do
  5.times do |i|
    value = queue.pop
    sleep rand(i/2) # simulate expense
    puts "consumed #{value}"
  end
end

consumer.join

Comments and changes to this ticket

  • Charles Comstock

    Charles Comstock July 9th, 2008 @ 06:48 PM

    • → Assigned user changed from “” to “Charles Comstock”
    • → State changed from “new” to “open”

    I fixed this but leaving this open because SizedQueue looks like it is still broken. Both of these need some spec love, but don't have the time at the moment to do so.

    I hacked in support for keeping track of the number of Threads waiting, but optimally it should just be fetched straight from the ConditionVariable.

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

People watching this ticket

Tags