O Reilly Ruby Cookbook source code

源代码在线查看: 03 - synchronizing access to an object.rb

软件大小: 292 K
上传用户: lz0324
关键词: Cookbook Reilly source Ruby
下载地址: 免注册下载 普通下载 VIP

相关代码

				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!				#---							

相关资源