2012年10月25日木曜日

rubyのthreadでエラーが出ない。



このロジック。
ruby 1.8.7だと、正常に動作。

indou@MacBook:work:$ rvm use 1.8.7
Using /Users/indou/.rvm/gems/ruby-1.8.7-p370
indou@MacBook:work:$ ruby thread0.rb
500
Square(500) = 22.3606797749979
300
Square(300) = 17.3205080756888
.
indou@MacBook:work:$ 

ruby 1.9.3だと、正常に動作しない。

indou@MacBook:work:$ rvm use 1.9.3
Using /Users/indou/.rvm/gems/ruby-1.9.3-p194
indou@MacBook:work:$ ruby thread0.rb
500
300
.
indou@MacBook:work:$ 

indou@MacBook:work:$ sdiff thread0.rb.bk20121025 thread0.rb
require "thread"                              require "thread"

q = Queue.new                                   q = Queue.new

t = Thread.new do                              t = Thread.new do
  loop do                                     loop do
    n = q.pop                                       n = q.pop
    if n.to_i >= 0                                  if n.to_i >= 0
      val = Math::sqrt(n)                          |           begin
      puts "Square(#{n}) = #{val}"                     |             val = Math::sqrt(n)
                                         >             puts "Square(#{n}) = #{val}"
                                         >           rescue Exception => e
                                         >             p e
                                         >           end
    else                                       else
      puts "?"                                         puts "?"
    end                                            end
  end                                          end
end                                        end

while line = gets                              while line = gets
  if line.chop! == "."                                if line.chop! == "."
    break                                       break
  else                                          else
    q.push(line)                                  q.push(line)
  end                                          end
end                                        end
indou@MacBook:work:$ 

indou@MacBook:work:$ ruby  thread0.rb
500
#
.
indou@MacBook:work:$ 

なんだ。Math::sqrt(n)でエラーになってる。なんで、エラー表示されないんだろう。
indou@MacBook:work:$ cat ts.rb

n = gets
p Math::sqrt(n)
indou@MacBook:work:$ ruby ts.rb
500
ts.rb:3:in `sqrt': can't convert String into Float (TypeError)
     from ts.rb:3:in `
'
indou@MacBook:work:$

スレッドがなければエラー表示される。 

indou@MacBook:work:$ sdiff thread0.rb.bk20121025.1 thread0.rb
require "thread"                              require "thread"

q = Queue.new                                   q = Queue.new

t = Thread.new do                              t = Thread.new do
  loop do                                     loop do
    n = q.pop                                       n = q.pop
    if n.to_i >= 0                                  if n.to_i >= 0
      begin                                    |           val = Math::sqrt(n.to_i)
        val = Math::sqrt(n)                          |           puts "Square(#{n}) = #{val}"
        puts "Square(#{n}) = #{val}"                     <
      rescue Exception => e                          <
        p e                                    <
      end                                    <
    else                                       else
      puts "?"                                         puts "?"
    end                                            end
  end                                          end
end                                        end

while line = gets                              while line = gets
  if line.chop! == "."                                if line.chop! == "."
    break                                       break
  else                                          else
    q.push(line)                                  q.push(line)
  end                                          end
end                                        end
indou@MacBook:work:$ 

indou@MacBook:work:$ ruby  thread0.rb
500
Square(500) = 22.360679774997898
300
Square(300) = 17.320508075688775
.
indou@MacBook:work:$ 




0 件のコメント: