1 #########################################
2 # The Computer Language Shootout #
3 # http://shootout.alioth.debian.org/ #
5 # Contributed by Jesse Millikan #
6 # Based on version by Gordon Innes #
7 #########################################
11 creature_meetings
= Queue
.new
12 meeting_point
= Mutex
.new
13 wait_signal
= ConditionVariable
.new
14 meetings_left
= ARGV[0].to_i
15 waiting_colour
, incoming_colour
= nil, nil
17 # Each chameneo is represented here by a thread
18 # and its colour variable, rather than explicitly
21 # This is all packed into one place for speed and
22 # clarity (It's clear to *me* :)
23 [:blue, :red, :yellow, :blue].each
{ |colour
|
27 # The form meeting_point.synchronize { } is slow
36 # Both threads emerge with variable other_colour set
38 other_colour
= waiting_colour
39 incoming_colour
= colour
44 waiting_colour
= colour
45 wait_signal
.wait(meeting_point
)
46 other_colour
= incoming_colour
52 # Take the complement colour
56 colour
== :red ? :yellow : :red
58 colour
== :blue ? :yellow : :blue
60 colour
== :blue ? :red : :blue
64 # Leave the total on the queue for the main thread
65 creature_meetings
.push(met
)
70 4.times
{ total
+= creature_meetings
.pop
}