Reputation:
I've been playing around with threads but I keep running into a problem where the treads seem to just die or stop.
Whats going on here? And how do I get round it?
I've included the code, but didn't paste it here as I think this problem is more fundamental to ruby. source code
thanks.
Edit Ruby 1.8, MacOS (snow leopard)
Upvotes: 0
Views: 1009
Reputation: 3268
If you've got threads randomly "going away", make sure you've set Thread.abort_on_exception
to true
. That'll stop the interpreter if an uncaught exception reaches the top-level in a background thread (via SystemExit
, so don't rescue Exception
or they'll get swallowed), and can be immensely useful in tracking down random bugs.
Upvotes: 0
Reputation: 7749
It looks like you're forgetting to add your new Thread objects to your threads object.
3.times do |t|
threads << Thread.new { word_list.process }
end
Your threads.each {|t| t.join} is working on an empty array, and so is ignoring the threads you did create. Make the change and it should wait.
Edit: I meant to << onto the array, not set it equal.
Upvotes: 3