Module: XThread
- Defined in:
- lib/xthread/monitor.rb,
lib/xthread.rb,
lib/xthread.rb
Overview
Adds monitor functionality to an arbitrary object by mixing the module with include
. For example:
require 'monitor'
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.
Defined Under Namespace
Modules: MonitorMixin, RBMonitorMixin Classes: RBMonitor, RBQueue, RBSizedQueue