From: "Ara.T.Howard" Date: 2004-05-06T02:23:57+09:00 Subject: Re: signaling another thread 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. -a -- =============================================================================== | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328 | URL :: http://www.ngdc.noaa.gov/stp/ | TRY :: for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done ===============================================================================