require 'thread' class Object def synchronize mutex.synchronize { yield self } end def mutex @mutex ||= Mutex.new end end #--- list = [] Thread.new { list.synchronize { |l| sleep(5); 3.times { l.push "Thread 1" } } } Thread.new { list.synchronize { |l| 3.times { l.push "Thread 2" } } } sleep(6) list # => ["Thread 1", "Thread 1", "Thread 1", "Thread 2", "Thread 2", "Thread 2"] #--- list = [] Thread.new { list.synchronize { |l| sleep(5); 3.times { l.push "Thread 1" } } } Thread.new { 3.times { list.push "Thread 2" } } sleep(6) list # => ["Thread 2", "Thread 2", "Thread 2", "Thread 1", "Thread 1", "Thread 1"] #--- require 'aspectr' require 'thread' class Synchronized < AspectR::Aspect def lock(method_sym, object, return_value, *args) object.mutex.lock end def unlock(method_sym, object, return_value, *args) object.mutex.unlock end end #--- array = %w{do re mi fa so la ti} Synchronized.new.wrap(array, :lock, :unlock, :push, :pop, :each) #--- Thread.new { array.each { |x| puts x } } Thread.new do puts 'Destroying the array.' array.pop until array.empty? puts 'Destroyed!' end # do # re # mi # fa # so # la # ti # Destroying the array. # Destroyed! #---