Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby thread programming , ruby equivalent of java wait/notify/notifyAll

I would like to know what are ruby's alternatives to the Java methods :

  • wait
  • notify
  • notifyAll

Could you please post a small snippet or some links ?

like image 746
Geo Avatar asked Feb 11 '09 00:02

Geo


2 Answers

What you are looking for is ConditionVariable in Thread:

require "thread"

m = Mutex.new 
c = ConditionVariable.new
t = []

t << Thread.new do
  m.synchronize do
    puts "A - I am in critical region"
    c.wait(m)
    puts "A - Back in critical region"
  end
end

t << Thread.new do
  m.synchronize do
    puts "B - I am critical region now"
    c.signal
    puts "B - I am done with critical region"
  end
end

t.each {|th| th.join }
like image 73
Sai Venkat Avatar answered Sep 28 '22 19:09

Sai Venkat


With the caveat that I don't know Java, based on your comments, I think that you want a condition variable. Google for "Ruby condition variable" comes up with a bunch of useful pages. The first link I get, seems to be a nice quick introduction to condition vars in particular, while this looks like it gives a much broader coverage of threaded programming in Ruby.

like image 40
womble Avatar answered Sep 28 '22 19:09

womble