Monitor#synchronize and Thread#wakeup


#1

e$BKNIt$G$9!#e(B

e$B0J2<$N%9%/%j%W%H$r<B9T$9$k$H!"<j85$G$Oe(Bt2e$B$N%k!<%We(B
e$B$,0l2s$7$+2s$i$J$$$N$G$9$,!"0U?^$5$l$?5sF0$J$s$Ge(B
e$B$7$g$&$+e(B?
"loop { t1.wakeup }“e$B$N9T$r%3%a%s%H%”%&%H$9$l$P!"e(B
e$B$A$c$s$HKhICe(Bt2e$B$,F0$$$F$k$3$H$r4QB,$G$-$k$N$G$9$,!#e(B

require “monitor”
extend MonitorMixin

def x(n)
synchronize do
x(n-1) unless n.zero?
end
end

t1 = Thread.start do
i = 0
loop do
x(i)
i += 1
end
end

t2 = Thread.start do
loop do
synchronize do
sleep 1
p Time.now
end
end
end

loop { t1.wakeup }
sleep