【问题标题】:Why does exiting a Ruby thread kill my whole program?为什么退出 Ruby 线程会杀死我的整个程序?
【发布时间】:2013-09-04 03:20:08
【问题描述】:

我有这段代码:

puts "Start"
loop do
    Thread.start do
        puts "Hello from thread"
        exit
    end
    text = gets
    puts "#{text}"
end
puts "Done"

我期望看到“开始”,然后是“来自线程的问候”,然后我可以输入会回显给我的输入。相反,我得到“Start”和“Hello from thread”,然后程序退出。

来自exit上的文档:

终止 thr 并安排另一个线程运行。如果这个线程 已经被标记为被杀死,退出返回线程。如果这是 主线程或最后一个线程退出进程。

但我认为我产生了一个新线程?为什么它会退出我的主进程?

【问题讨论】:

    标签: ruby multithreading


    【解决方案1】:

    您正在查看Thread#exit 文档。 kill 是终止 Ruby 脚本的 Kernel#exit

    puts "Start"
    loop do
        Thread.start do
            puts "Hello from thread"
            Thread.exit
        end
        text = gets
        puts "#{text}"
    end
    puts "Done"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-06
      • 1970-01-01
      • 2017-08-05
      相关资源
      最近更新 更多