mutex

http://ruby-doc.org/core-1.9.3/Mutex.html

 

Mutex implements a simple semaphore that can be used to coordinate access to shared data from multiple concurrent threads.

Example:

require 'thread'
semaphore = Mutex.new

a = Thread.new {
  semaphore.synchronize {
    # access shared resource
  }
}

b = Thread.new {
  semaphore.synchronize {
    # access shared resource
  }
}
 
 
def synchronize
   self.lock
   begin
      yield
   ensure
      self.unlock rescue nil
   end
end
 

Example without Mutax:

#!/usr/bin/rubyrequire'thread'

count1 = count2 =0
difference =0
counter =Thread.new do
   loop do
      count1 +=1
      count2 +=1
    end
end
spy =Thread.new do
   loop do
      difference +=(count1 - count2).abs
   end
end
sleep 1
puts "count1 :  #{count1}"
puts "count2 :  #{count2}"
puts "difference : #{difference}"

This will produce the following result:

count1 :1583766
count2 :1583766
difference :637992
#!/usr/bin/rubyrequire'thread'
mutex =Mutex.new

count1 = count2 =0
difference =0
counter =Thread.new do
   loop do
      mutex.synchronize do
         count1 +=1
         count2 +=1
      end
    end
end
spy =Thread.newdo
   loop do
       mutex.synchronize do
          difference +=(count1 - count2).abs
       end
    end
end
sleep 1
mutex.lock
puts "count1 :  #{count1}"
puts "count2 :  #{count2}"
puts "difference : #{difference}"

This will produce the following result:

count1 :696591
count2 :696591
difference :0

Handling Deadlock:

When we start using Mutex objects for thread exclusion we must be careful to avoid deadlock. Deadlock is the condition that occurs when all threads are waiting to acquire a resource held by another thread. Because all threads are blocked, they cannot release the locks they hold. And because they cannot release the locks, no other thread can acquire those locks.

This is where condition variables come into picture. A condition variable is simply a semaphore that is associated with a resource and is used within the protection of a particular mutex. When you need a resource that's unavailable, you wait on a condition variable. That action releases the lock on the corresponding mutex. When some other thread signals that the resource is available, the original thread comes off the wait and simultaneously regains the lock on the critical region.

Example:

#!/usr/bin/rubyrequire'thread'
mutex =Mutex.new

cv =ConditionVariable.new
a =Thread.new{
   mutex.synchronize {
      puts "A: I have critical section, but will wait for cv"
      cv.wait(mutex)
      puts "A: I have critical section again! I rule!"}}

puts "(Later, back at the ranch...)"

b =Thread.new{
   mutex.synchronize {
      puts "B: Now I am critical, but am done with cv"
      cv.signal
      puts "B: I am still critical, finishing up"}}
a.join
b.join

This will produce the following result:

A: I have critical section, but will wait for cv
(Later, back at the ranch...)
B:Now I am critical, but am donewith cv
B: I am still critical, finishing up
A: I have critical section again! I rule!
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值