Module

MonitorMixin

Adds monitor functionality to an arbitrary object by mixing the module with include. For example:

require 'monitor.rb'

buf = []
buf.extend(MonitorMixin)
empty_cond = buf.new_cond

# consumer
Thread.start do
  loop do
    buf.synchronize do
      empty_cond.wait_while { buf.empty? }
      print buf.shift
    end
  end
end

# producer
while line = ARGF.gets
  buf.synchronize do
    buf.push(line)
    empty_cond.signal
  end
end

The consumer thread waits for the producer thread to push a line to buf while buf.empty?, and the producer thread (main thread) reads a line from ARGF and push it to buf, then call empty_cond.signal.

Classes
ConditionVariable FIXME: This isn’t documented in Nutshell.
Public Methods
extend_object
mon_enter Enters exclusive section.
mon_exit Leaves exclusive section.
mon_synchronize Enters exclusive section and executes the block. Leaves the exclusive section automatically when the block exits. See example under MonitorMixin.
mon_try_enter Attempts to enter exclusive section. Returns false if lock fails.
new
new_cond FIXME: This isn’t documented in Nutshell.
synchronize Alias for #mon_synchronize
try_mon_enter Alias for #mon_try_enter
Private Methods
mon_acquire
mon_check_owner Throw a ThreadError exception if the current thread does’t own the monitor
mon_enter_for_cond
mon_exit_for_cond
mon_initialize called by initialize method to set defaults for instance variables.
mon_release
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.