Class: RecursiveMutex
- Inherits:
-
Mutex
- Object
- Mutex
- RecursiveMutex
- Defined in:
- lib/thread/recursive_mutex.rb
Overview
A recursive mutex lets you lock in various threads recursively, allowing you to do multiple locks one inside another.
You really shouldn’t use this, but in some cases it makes your life easier.
Instance Method Summary collapse
-
#initialize ⇒ RecursiveMutex
constructor
A new instance of RecursiveMutex.
-
#lock ⇒ Object
Lock the mutex.
-
#unlock ⇒ Object
Unlock the mutex.
Constructor Details
#initialize ⇒ RecursiveMutex
Returns a new instance of RecursiveMutex.
18 19 20 21 22 23 |
# File 'lib/thread/recursive_mutex.rb', line 18 def initialize @threads_lock = Mutex.new @threads = Hash.new { |h, k| h[k] = 0 } super end |
Instance Method Details
#lock ⇒ Object
Lock the mutex.
26 27 28 |
# File 'lib/thread/recursive_mutex.rb', line 26 def lock super if @threads_lock.synchronize{ (@threads[Thread.current] += 1) == 1 } end |