From: ahoward Date: 2003-02-27T03:16:19+09:00 Subject: Re: Ruby in Performance Testing On Wed, 26 Feb 2003, Robert Klemme wrote: > 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. Thread logging would be a good addition to the thread module IMHO... something similar to what you've written... -a > > 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) > > -- ==================================== | Ara Howard | NOAA Forecast Systems Laboratory | Information and Technology Services | Data Systems Group | R/FST 325 Broadway | Boulder, CO 80305-3328 | Email: ahoward@fsl.noaa.gov | Phone: 303-497-7238 | Fax: 303-497-7259 ====================================