From: "James M. Lawrence" Date: 2009-03-09T13:18:32+09:00 Subject: Re: 1.9.x Thread problem Here is a bare solution using a mutex and a counter. The next thing would be to use a condition variable to put the main thread to sleep while the child threads initialize, rather than spinning wheels / burning rubber while waiting. Also look into monitor.rb, sync.rb, and producer/consumer strategies. require 'thread' THREADS = 10 parent_id = Thread.current.object_id mutex = Mutex.new num_threads_ready = 0 queue = Queue.new THREADS.times do |i| queue << Thread.new do th = Thread.current th[:index] = i mutex.synchronize { num_threads_ready += 1 } # <----- raise "Running inside parent" if th.object_id == parent_id sleep(2) end end # wait until threads have initialized until mutex.synchronize { num_threads_ready == THREADS } Thread.pass end THREADS.times do th = queue.pop raise "Running inside parent" if th.object_id == parent_id puts "Joining thread #{th[:index]}" th.join() end -- Posted via http://www.ruby-forum.com/.