Fix for JRUBY-2882. Handle error messages related to constructors better
[jruby.git] / bench / shootout / prodcons.ruby
blob209af2494d5102e623897e43f1112ad4597f7335
1 #!/usr/bin/ruby
2 # -*- mode: ruby -*-
3 # $Id: prodcons.ruby,v 1.3 2005-06-10 00:57:22 igouy-guest Exp $
4 # http://www.bagley.org/~doug/shootout/
6 require 'thread'
8 def main(n)
9 mutex = Mutex.new
10 access = ConditionVariable.new
11 count = data = consumed = produced = 0
12 consumer = Thread.new do
13 i = 0
14 loop do
15 mutex.synchronize {
16 while count == 0 do access.wait(mutex) end
17 i = data
18 count = 0
19 access.signal
21 consumed += 1
22 if i == n then break end
23 end
24 end
25 producer = Thread.new do
26 for i in 1 .. n do
27 mutex.synchronize {
28 while count == 1 do access.wait(mutex) end
29 data = i
30 count = 1
31 access.signal
33 produced += 1
34 end
35 end
36 producer.join
37 consumer.join
38 puts "#{produced} #{consumed}"
39 end
41 main(Integer(ARGV.shift || 1))