* lib/monitor.rb: fixed the example code.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@1234 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
a36e0c78c9
commit
93868607a2
@ -1,27 +1,44 @@
|
|||||||
=begin
|
=begin
|
||||||
|
|
||||||
monitor.rb
|
= monitor.rb
|
||||||
Author: Shugo Maeda <shugo@netlab.co.jp>
|
|
||||||
Version: 1.2.1
|
|
||||||
|
|
||||||
USAGE:
|
Copyright (C) 2001 Shugo Maeda <shugo@ruby-lang.org>
|
||||||
|
|
||||||
foo = Foo.new
|
This library is distributed under the terms of the Ruby license.
|
||||||
foo.extend(MonitorMixin)
|
You can freely distribute/modify this library.
|
||||||
cond = foo.new_cond
|
|
||||||
|
|
||||||
thread1:
|
== example
|
||||||
foo.synchronize {
|
|
||||||
...
|
|
||||||
cond.wait_until { foo.done? }
|
|
||||||
...
|
|
||||||
}
|
|
||||||
|
|
||||||
thread2:
|
This is a simple example.
|
||||||
foo.synchronize {
|
|
||||||
foo.do_something
|
require 'monitor.rb'
|
||||||
cond.signal
|
|
||||||
}
|
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.
|
||||||
|
|
||||||
=end
|
=end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user