📄 10 - avoiding deadlock.rb
字号:
require 'thread'$chopstick1 = Mutex.new$chopstick2 = Mutex.newclass Philosopher < Thread def initialize(name) super do loop do $chopstick1.synchronize do puts "#{name} has picked up one chopstick." $chopstick2.synchronize do puts "#{name} has picked up two chopsticks and eaten a " + "bite of tasty rice." end end end end endendPhilosopher.new('Moore')Philosopher.new('Anscombe')# Moore has picked up one chopstick.# Moore has picked up two chopsticks and eaten a bite of tasty rice.# Anscombe has picked up one chopstick.# Anscombe has picked up two chopsticks and eaten a bite of tasty rice.# Moore has picked up one chopstick.# Moore has picked up two chopsticks and eaten a bite of tasty rice.# ...#---require 'thread'pool_lock, lion_lock, penguin_lock, cabbage_lock = (1..4).collect { Mutex.new }locks = [pool_lock, lion_lock, penguin_lock, cabbage_lock]$lock_order = {}locks.each_with_index { |lock, i| $lock_order[lock] = i }def lock_all(*locks) ordered_locks = locks.sort_by { |x| $lock_order[x] } ordered_locks.each do |lock| puts "Locking #{$lock_order[lock]}." if $DEBUG lock.lock end begin yield ensure ordered_locks.reverse_each do |lock| puts "Unlocking #{$lock_order[lock]}." if $DEBUG lock.unlock end endend#---$DEBUG = truelock_all(penguin_lock, pool_lock) do puts "I'm putting the penguin in the pool."end# Locking 0.# Locking 2.# I'm putting the penguin in the pool.# Unlocking 2.# Unlocking 0.#---require 'thread'$lock = Mutex.newThread.new do $lock.synchronize { $lock.synchronize { puts 'I synchronized twice!' } }end#---
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -