diff options
| author | shugo <shugo@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2001-03-06 09:03:59 +0000 |
|---|---|---|
| committer | shugo <shugo@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2001-03-06 09:03:59 +0000 |
| commit | c2bceaefcc24b6d614e75b4cfc65a6dd5cb54a7a (patch) | |
| tree | b2d585c234081a94a4e60975bfe501b85c1b2be3 /lib/monitor.rb | |
| parent | 19712e884e98a1fd128e6b869db0d42197227293 (diff) | |
| download | ruby-c2bceaefcc24b6d614e75b4cfc65a6dd5cb54a7a.tar.gz ruby-c2bceaefcc24b6d614e75b4cfc65a6dd5cb54a7a.tar.xz ruby-c2bceaefcc24b6d614e75b4cfc65a6dd5cb54a7a.zip | |
* lib/monitor.rb: fixed the example code.
git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@1234 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/monitor.rb')
| -rw-r--r-- | lib/monitor.rb | 53 |
1 files changed, 35 insertions, 18 deletions
diff --git a/lib/monitor.rb b/lib/monitor.rb index 75d9c3582..7cca6e871 100644 --- a/lib/monitor.rb +++ b/lib/monitor.rb @@ -1,27 +1,44 @@ =begin -monitor.rb -Author: Shugo Maeda <shugo@netlab.co.jp> -Version: 1.2.1 += monitor.rb -USAGE: +Copyright (C) 2001 Shugo Maeda <shugo@ruby-lang.org> - foo = Foo.new - foo.extend(MonitorMixin) - cond = foo.new_cond +This library is distributed under the terms of the Ruby license. +You can freely distribute/modify this library. - thread1: - foo.synchronize { - ... - cond.wait_until { foo.done? } - ... - } +== example - thread2: - foo.synchronize { - foo.do_something - cond.signal - } +This is a simple 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. =end |
