Ruby线程致命死锁错误

时间:2012-02-01 18:55:21

标签: ruby multithreading

我有这个简单的红宝石代码:

require 'rubygems'
require 'Thread'


queue = Queue.new

t = Thread.new do
  loop do
    user = queue.pop

    puts "Unqueued #{user.nickname}, running all scripts..."

    #do stuff
    user.run_all

    puts "Finished running all script for  #{user.nickname}, sleeping for 2"
    sleep 2

    queue << user    
  end
end

t.join

当我尝试运行它时,它给了我这个错误:

deadlock 0x10ec549f0: sleep:-  - ./Machine.rb:9
deadlock 0x10ea46348: sleep:J(0x10ec549f0) (main) - ./Machine.rb:23
fatal: Thread(0x10ea46348): deadlock
    from ./Machine.rb:23:in `join'
    from ./Machine.rb:23
    from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:36:in `gem_original_require'
    from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:36:in `require'
    from (irb):1

我做错了什么? 感谢

1 个答案:

答案 0 :(得分:5)

您正尝试从队列中弹出一些内容,并且队列为空。通常你的线程会锁定,等待另一个线程在那里推送一些东西。但除了等待你的线程完成的主进程之外,没有其他线程。那么你期望Ruby做什么呢?