Class: Thread::Monitor
| Relationships & Source Files | |
| Namespace Children | |
|
Classes:
| |
| Inherits: | Object |
| Defined in: | thread_sync.rb |
Overview
Use the Monitor class when you want to have a lock object for blocks with mutual exclusion.
lock = Monitor.new
lock.synchronize do
# exclusive access
end
Contrary to Mutex, Monitor is reentrant:
lock = Monitor.new
lock.synchronize do
lock.synchronize do
# exclusive access
end
end
Instance Attribute Summary
- #mon_locked? ⇒ Boolean readonly Internal use only
- #mon_owned? ⇒ Boolean readonly Internal use only
Instance Method Summary
-
#enter ⇒ nil
Enters exclusive section.
-
#exit ⇒ nil
Leaves exclusive section.
-
#new_cond
Creates a new
ConditionVariableassociated with theMonitorobject. -
#synchronize ⇒ result of the block
Enters exclusive section and executes the block.
-
#try_enter ⇒ Boolean
Attempts to enter exclusive section.
-
#mon_check_owner
Internal use only
internal methods for MonitorMixin.
-
#wait_for_cond(cond, timeout)
Internal use only
internal methods for
MonitorMixin::ConditionVariable
Instance Attribute Details
#mon_locked? ⇒ Boolean (readonly)
# File 'thread_sync.rb', line 589
def mon_locked? # :nodoc: Primitive.rb_monitor_locked_p end
#mon_owned? ⇒ Boolean (readonly)
# File 'thread_sync.rb', line 593
def mon_owned? # :nodoc: Primitive.rb_monitor_owned_p end
Instance Method Details
#enter ⇒ nil
Enters exclusive section.
# File 'thread_sync.rb', line 572
def enter Primitive.rb_monitor_enter end
#exit ⇒ nil
Leaves exclusive section.
# File 'thread_sync.rb', line 580
def exit Primitive.rb_monitor_exit end
#mon_check_owner
internal methods for MonitorMixin
# File 'thread_sync.rb', line 585
def mon_check_owner # :nodoc: Primitive.rb_monitor_check_owner end
#new_cond
Creates a new Monitor::ConditionVariable associated with the Monitor object.
# File 'thread_sync.rb', line 605
def new_cond ConditionVariable.new(self) end
#synchronize ⇒ result of the block
Enters exclusive section and executes the block. Leaves the exclusive section automatically when the block exits. See example under MonitorMixin.
# File 'thread_sync.rb', line 556
def synchronize(&) Primitive.rb_monitor_synchronize end
#try_enter ⇒ Boolean
Attempts to enter exclusive section. Returns false if lock fails.
# File 'thread_sync.rb', line 564
def try_enter Primitive.rb_monitor_try_enter end
#wait_for_cond(cond, timeout)
internal methods for MonitorMixin::ConditionVariable
# File 'thread_sync.rb', line 598
def wait_for_cond(cond, timeout) # :nodoc: Primitive.rb_monitor_wait_for_cond(cond, timeout) end