Ruby ruby thread deadlock detected

匿名 · 2012年10月04日 · 最后由 lyfi2003 回复于 2012年10月04日 · 3841 次阅读

t = Thread.new do puts "[Starting thread]" Thread.stop puts "[Resuming thread]" end puts "Status of thread: #{t.status}" puts "Is thread stopped? #{t.stop?}" puts "Is thread alive? #{t.alive?}"

puts puts "Waking up thread and joining it..." t.wakeup t.join puts puts "Is thread alive? #{t.alive?}" puts "Inspect string for the thread: #{t.inspect}"

运行这段代码很多次,只成功一两次, Status of thread: run Is thread stopped? false Is thread alive? true

Waking up thread and joining it... [Starting thread] test.rb:13:in join': deadlock detected (fatal) from test.rb:13:in

你需要贴出来你的 Ruby 版本,用的什么环境。

直观从代码看,用法是没有问题的。

因为你 join 一个 stop 的线程了。 注意你的输出 你 wakeup 的时候线程还没有停止 所以之后你 join 等待线程。再之后这个线程 stop 了,当然死锁 在你 wakeup 前 sleep 几秒等线程执行到 stop 就正常了

#2 楼 @jjym 观察仔细。也跟 Ruby 的线程模型有关系。

匿名 #4 2012年10月04日

#1 楼 @lyfi2003 the well grounded rubyist, page 427

匿名 #5 2012年10月04日

#1 楼 @lyfi2003 ruby 1.9.3p0 (2011-10-30 revision 33570) [i686-linux]

#5 楼 @nuclearkitten @jjym 已经发现问题了,你在 t.wakeup 前面加上 sleep 2 试下。原因如 2 楼所示

需要 登录 后方可回复, 如果你还没有账号请 注册新账号