- Inheritance
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 & Modules
Methods
Class
| Visibility | Signature |
|---|---|
| public | extend_object (obj) |
| public | new (*args) |
Instance
| Visibility | Signature |
|---|---|
| public | mon_enter () |
| public | mon_exit () |
| public | mon_synchronize () {|| ...} |
| public | mon_try_enter () |
| public | new_cond () |
| public | synchronize () |
| public | try_mon_enter () |
Class Method Detail
extend_object(obj)
new(*args)
Instance Method Detail
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_cond()
FIXME: This isn‘t documented in Nutshell.
Create a new condition variable for this monitor. This facilitates control of the monitor with signal and wait.
synchronize()
Alias for mon_synchronize
try_mon_enter()
Alias for mon_try_enter