class Thread::Monitor

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

Public Instance Methods

enter → nil click to toggle source

Enters exclusive section.

# File thread_sync.rb, line 572
def enter
  Primitive.rb_monitor_enter
end
exit → nil click to toggle source

Leaves exclusive section.

# File thread_sync.rb, line 580
def exit
  Primitive.rb_monitor_exit
end
new_cond() click to toggle source

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 click to toggle source

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 → true or false click to toggle source

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