Ruby Multithreading: making one thread wait for a signal from another

Posted by Peter on Stack Overflow See other posts from Stack Overflow or by Peter
Published on 2010-03-29T20:50:06Z Indexed on 2010/03/29 20:53 UTC
Read the original article Hit count: 330

Filed under:
|

In Ruby, I want to have two threads running at the same time, and want the background thread to periodically signal the foreground thread. How do I get the foreground thread to block until the background thread says 'go'? I can think of a few ways to do it, but am after the most appropriate, idiomatic Ruby method.

In code:

loop do  # background, thread 1
  sleep 3
  receive_input
  # tell foreground input is ready # <-- how do I do this?
end

and

loop do  # foreground, thread 2
  wait_for_signal_from_background  # <-- how do I do this?
  do_something
end

© Stack Overflow or respective owner

Related posts about ruby

Related posts about multithreading