From: Robert Klemme Date: 2003-02-27T02:36:03+09:00 Subject: Re: Ruby in Performance Testing "ahoward" schrieb im Newsbeitrag news:Pine.LNX.4.53.0302261440230.14239@eli.fsl.noaa.gov... > i'm not sure, but accessing stdout/stderr from this many threads at once may > be a problem - i've had problems with it before. one thing i've done in my > threaded problems is something like this : In order to get a grip on ruby threading I implemented a similar version that does also write the output along the way. Feel free to use and modify it as you like. robert require 'thread' class Queue def initialize() @q = [] @mutex = Mutex.new @cond = ConditionVariable.new end def enqueue(*elems) @mutex.synchronize do @q.push *elems @cond.signal end end def dequeue() @mutex.synchronize do while @q.empty? do @cond.wait(@mutex) end return @q.shift end end end class Logger def initialize(io, close=false) @closeOnTerminate = close @queue = Queue.new @io = io @t = Thread.new do while true do @io.puts(@queue.dequeue) end end end def log(*args) checkRunning @queue.enqueue(*args) end def terminate() checkRunning @t.exit @t = nil @io.close if @closeOnTerminate @io = nil @queue = nil end def checkRunning raise "Logger already terminated" unless @t end end l = Logger.new($stdout) l.log("hello") l.log("this is it") l.log(%w{this is it}) l.terminate sleep(5)