From: Robert Klemme Date: 2004-05-06T06:23:57+09:00 Subject: Re: signaling another thread "Ara.T.Howard" schrieb im Newsbeitrag news:Pine.LNX.4.44.0405051113590.30055-100000@fattire.ngdc.noaa.gov... > On Thu, 6 May 2004, Joel VanderWerf wrote: > > > Ara.T.Howard wrote: > > > > > > what is is the prefered method in which one thread can 'signal' another thread > > > on some condition? Observable? other? > > > > One way is to use Thread#raise: > > > > $ cat thread-signal.rb > > class PayAttention < Exception; end > > > > other = Thread.new do > > i=0 > > begin > > loop do > > sleep 1 > > i += 1 # important work > > end > > rescue PayAttention > > puts i > > retry if i < 10 > > end > > end > > > > watch = Thread.new do > > loop do > > sleep 3 > > other.raise PayAttention > > end > > end > > > > other.join > > > > $ ruby thread-signal.rb > > 2 > > 4 > > 6 > > 8 > > 10 > > $ > > nice - i'll take it. thanks. It depends on the situation: if the thread to notify may be idle, a ConditionVariable is better. see: http://www.ruby-doc.org/docs/rdoc/1.9/classes/ConditionVariable.html http://www.rubygarden.org/ruby?MultiThreading That spares you the sleeping and is the more natural and resource saving approach. Regards robert