From: "Townley, Andrew" Date: 2005-08-31T08:40:02+09:00 Subject: Rogue threads found! The plot thickens.... (was: Re: ThreadPool(was: Re: Thread.list confusion)) On Tue, 2005-08-30 at 19:05, Eric Hodel wrote: [lots of really useful feedback deleted] Thanks for the feedback, Eric. I'd actually made some other changes to it prior to getting your comments. I have to say, looking at the difference, I can see some value in the ThreadGroup, but I don't really get why you're so excited about it... this is for another day, I think. In other news, I found the mystery multiplying threads and have a problem that I'm not quite sure how to solve with the tools at hand... Maybe someone out there can help, but maybe the answer is to just roll my own something-or-other again. The problem came from two seemingly benign blocks of code: $ cat fu.rb require 'thread' require 'timeout' @queue = Queue.new def read(timeout) begin Timeout::timeout(timeout) do puts("READ: #{@queue.length} elements; #{@queue.num_waiting} threads waiting.") return @queue.deq end rescue Timeout::Error puts("TIMEOUT: #{@queue.length} elements; #{@queue.num_waiting} threads waiting.") end end and this (/usr/lib/ruby/1.8/thread.rb): 272 # 273 # Retrieves data from the queue. If the queue is empty, the calling thread is 274 # suspended until data is pushed onto the queue. If +non_block+ is true, the 275 # thread isn't suspended, and an exception is raised. 276 # 277 def pop(non_block=false) 278 while (Thread.critical = true; @que.empty?) 279 raise ThreadError, "queue empty" if non_block 280 @waiting.push Thread.current 281 Thread.stop 282 end 283 @que.shift 284 ensure 285 Thread.critical = false 286 end 287 alias shift pop 288 alias deq pop because of this (/usr/lib/ruby/1.8/timeout.rb): 32 module Timeout 33 class Error