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_ |
|
| mon_ |
Enters exclusive section. |
| mon_ |
Leaves exclusive section. |
| mon_ |
Enters exclusive section and executes the block. Leaves the exclusive section automatically when the block exits. See example under MonitorMixin. |
| mon_ |
Attempts to enter exclusive section. Returns false if lock fails. |
| new | |
| new_ |
FIXME: This isn’t documented in Nutshell. |
| synchronize | Alias for #mon_synchronize |
| try_ |
Alias for #mon_try_enter |
| Private Methods | |
|---|---|
| mon_ |
|
| mon_ |
Throw a ThreadError exception if the current thread does’t own the monitor |
| mon_ |
|
| mon_ |
|
| mon_ |
called by initialize method to set defaults for instance variables. |
| mon_ |
|
<code/>and<pre/>for code samples.