123456789_123456789_123456789_123456789_123456789_

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

Instance Method Summary

Instance Attribute Details

#mon_locked?Boolean (readonly)

This method is for internal use only.
[ GitHub ]

  
# File 'thread_sync.rb', line 589

def mon_locked? # :nodoc:
  Primitive.rb_monitor_locked_p
end

#mon_owned?Boolean (readonly)

This method is for internal use only.
[ GitHub ]

  
# File 'thread_sync.rb', line 593

def mon_owned? # :nodoc:
  Primitive.rb_monitor_owned_p
end

Instance Method Details

#enternil

Enters exclusive section.

[ GitHub ]

  
# File 'thread_sync.rb', line 572

def enter
  Primitive.rb_monitor_enter
end

#exitnil

Leaves exclusive section.

[ GitHub ]

  
# File 'thread_sync.rb', line 580

def exit
  Primitive.rb_monitor_exit
end

#mon_check_owner

This method is for internal use only.

internal methods for MonitorMixin

[ GitHub ]

  
# 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.

[ GitHub ]

  
# File 'thread_sync.rb', line 605

def new_cond
  ConditionVariable.new(self)
end

#synchronizeresult of the block

Enters exclusive section and executes the block. Leaves the exclusive section automatically when the block exits. See example under MonitorMixin.

[ GitHub ]

  
# File 'thread_sync.rb', line 556

def synchronize(&)
  Primitive.rb_monitor_synchronize
end

#try_enterBoolean

Attempts to enter exclusive section. Returns false if lock fails.

[ GitHub ]

  
# File 'thread_sync.rb', line 564

def try_enter
  Primitive.rb_monitor_try_enter
end

#wait_for_cond(cond, timeout)

This method is for internal use only.

internal methods for MonitorMixin::ConditionVariable

[ GitHub ]

  
# File 'thread_sync.rb', line 598

def wait_for_cond(cond, timeout) # :nodoc:
  Primitive.rb_monitor_wait_for_cond(cond, timeout)
end