Maybe I do not quite understand your question, but I guess ConditionVariable
is a good approach for such problem.
So, ConditionVariable
can be used to signal threads when something happens. Let's see:
require 'thread'
a = [] # array a is empty now
mutex = Mutex.new
condvar = ConditionVariable.new
Thread.new do
mutex.synchronize do
sleep(5)
a << "Hey hey!"
# Now we have value in array; it's time to signal about it
condvar.signal
end
end
mutex.synchronize do
condvar.wait(mutex)
# This happens only after 5 seconds, when condvar recieves signal
puts "Hey. Array a is not empty now!"
end