开发者

Ruby Threads Die?

开发者 https://www.devze.com 2023-02-27 06:15 出处:网络
I\'ve been playing around with threads but I keep开发者_StackOverflow running into a problem where the treads seem to just die or stop.

I've been playing around with threads but I keep开发者_StackOverflow 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)


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消