Sleep with wakeup

Hello,
I need loop in a thread (A), with a sleep, and
sometime, in another thread, wakeup A on event.

I do that, but wonder if there are not a best method
with select/mutex/conditional variable

=====================
require ‘timeout’

th=Thread.new { loop {
ev=timeout(3) { Thread.stop ; true} rescue false
p “do work ev=#{ev} #{Time.now}”
} }

while gets
p “wakeup”
th.wakeup
end

Ok, find that :

========================
mutex = Mutex.new
cv = ConditionVariable.new

th=Thread.new { loop {
ev=mutex.synchronize { cv.wait(mutex,4) }
p “do work ev=#{ev} #{Time.now}”
} }

while $stdin.gets
p “wakeup”
mutex.synchronize { cv.broadcast() }
end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs