Mutex deadlock error, but no deadlock possible, no hazards

Ruby complains of a deadlock situation with the mutexes. I don’t see
any hazards or deadlocks - am I missing something?

./mutex_brokenq.rb
two threads are searching …
internal:prelude:8:in lock': deadlock; recursive locking (ThreadError) from <internal:prelude>:8:insynchronize’
from ./mutex_brokenq.rb:33:in `’


here is the code:
#!/bin/ruby

puts “two threads are searching …”

wait_for_finish = Mutex.new
wait_for_finish.lock

wait_for_finish_mutex = Mutex.new

thread_one = Thread.new {
  puts "thread_one working!"
  count=0
  while( count < 10000)
    count += 1
  end
  wait_for_finish_mutex.synchronize do
    if wait_for_finish.locked? then wait_for_finish.unlock end
  end
}

thread_two = Thread.new {
  puts "thread_two working!"
  count=0
  while( count < 9000)
    count += 1
  end
  wait_for_finish_mutex.synchronize do
    if wait_for_finish.locked? then wait_for_finish.unlock end
  end
}

wait_for_finish.synchronize do
puts “Eureka!”
end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs