I'm having a problem right now where I can't see where my child threads are spitting out error messages which is making it difficult to debug.
eg:
Thread.new{
a = 1/0
}
Is there any way to have all thread errors print out at stderr?
In standard Ruby implementations, an Array is not thread-safe.
Ruby makes it easy to write multi-threaded programs with the Thread class. Ruby threads are a lightweight and efficient way to achieve concurrency in your code.
Unfortunately, Ruby doesn't ship with any thread-safe Array or Hash implementations. The core Array and Hash classes are not thread-safe by default, nor should they be.
Multi-threading is the most useful property of Ruby which allows concurrent programming of two or more parts of the program for maximizing the utilization of CPU. Each part of a program is called Thread.
Set the Thread class' abort_on_exception
flag to true.
Alternately, wrap the thread body in a throw/catch block and dump the exception in the catch.
Set $DEBUG
to true (you can do this from the command line with -d
), and you'll get
ruby -d bad_thread.rb
Exception `LoadError' at /usr/lib/ruby/site_ruby/1.8/rubygems.rb:1113 - no such file to load -- rubygems/defaults/operating_system
Exception `LoadError' at /usr/lib/ruby/site_ruby/1.8/rubygems/config_file.rb:34 - no such file to load -- Win32API
Exception `ZeroDivisionError' at bad_thread.rb:2 - divided by 0
bad_thread.rb:2:in `/': divided by 0 (ZeroDivisionError)
from bad_thread.rb:2
from bad_thread.rb:1:in `initialize'
from bad_thread.rb:1:in `new'
from bad_thread.rb:1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With