From: Joel VanderWerf Date: 2006-07-06T07:21:42+09:00 Subject: Re: thread mystery ara.t.howard@noaa.gov wrote: > > any idea why this script slows downs drastically as it runs and seems to > leak > memory? No idea, except that it seems to be related to the thread creation (maybe because it's within the sync block). The following works around it, and is probably more efficient anyway. I guess the semantics are close to the original. require 'sync' require 'thread' class Switch ON, OFF, NEITHER = true, false, nil def initialize state = OFF extend Sync_m @state = NEITHER @observers = [] end def on! synchronize(:EX){ warn 'on!' @state = ON notity_observers } end def off! synchronize(:EX){ warn 'off!' @state = OFF notity_observers } end def notity_observers synchronize(:SH){ @observers.each do |o| o.notify @state end } end def add_observer o synchronize(:EX){ @observers << o } end end class SwitchToggle def initialize switch @switch = switch @switch.add_observer self @q = Queue.new Thread.new do loop {@q.pop.call} end end def notify of case of when Switch::ON later{ @switch.off! } when Switch::OFF later{ @switch.on! } else raise of.to_s end end def later(&bl) @q << bl end end switch = Switch.new toggle = SwitchToggle.new switch switch.on! STDIN.gets -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407