From 3ed1979de25fad04b2b74bd4e4de13d7edd94b3d Mon Sep 17 00:00:00 2001 From: "(no author)" <(no author)@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> Date: Tue, 20 Mar 2001 14:50:43 +0000 Subject: This commit was manufactured by cvs2svn to create tag 'v1_6_3'. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/tags/v1_6_3@1265 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/monitor.rb | 53 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 18 deletions(-) (limited to 'lib/monitor.rb') diff --git a/lib/monitor.rb b/lib/monitor.rb index 75d9c35821..7cca6e871e 100644 --- a/lib/monitor.rb +++ b/lib/monitor.rb @@ -1,27 +1,44 @@ =begin -monitor.rb -Author: Shugo Maeda -Version: 1.2.1 += monitor.rb -USAGE: +Copyright (C) 2001 Shugo Maeda - 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 -- cgit v1.2.3