From: Robert Klemme Date: 2011-05-13T17:42:20+09:00 Subject: Re: help on threads synchronization On Fri, May 13, 2011 at 9:33 AM, Zd Yu wrote: > How to make two threads do something in turn, i.e., A => B => A => B > ....? > > I know the code below is not correct, but I do not know how to make it > correct. Could anybody help? > > ======================================= > mutex=Mutex.new > > t=Thread.new { >  while true >    Thread.stop >    puts "B move..." >    Thread.main.wakeup >  end > } > > while true >  puts "A move..." >  t.wakeup >  Thread.stop > end Since you want to have one thread active at a time only this is not a case for threads. If at all you might use Fibers: http://www.rubyinside.com/ruby-fibers-8-useful-reads-on-rubys-new-concurrency-feature-1769.html http://www.ruby-doc.org/core-1.9/classes/Fiber.html The simplest solution is to code interchanges directly: Actor = Struct.new :number do def alive?; rand(10) != 0 end def step printf "Actor %p working...\n", number end end acts = Array.new(2) {|i| Actor.new i } until acts.empty? acts.delete_if do |a| a.step !a.alive? end end Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/