[macruby-changes] [2669] MacRuby/trunk/sample-macruby/Scripts/sleeping_barber.rb

source_changes at macosforge.org source_changes at macosforge.org
Mon Sep 28 19:13:51 PDT 2009


Revision: 2669
          http://trac.macosforge.org/projects/ruby/changeset/2669
Author:   lsansonetti at apple.com
Date:     2009-09-28 19:13:48 -0700 (Mon, 28 Sep 2009)
Log Message:
-----------
added barber.rb, a GCD-based implementation of the sleeping barber problem

Added Paths:
-----------
    MacRuby/trunk/sample-macruby/Scripts/sleeping_barber.rb

Added: MacRuby/trunk/sample-macruby/Scripts/sleeping_barber.rb
===================================================================
--- MacRuby/trunk/sample-macruby/Scripts/sleeping_barber.rb	                        (rev 0)
+++ MacRuby/trunk/sample-macruby/Scripts/sleeping_barber.rb	2009-09-29 02:13:48 UTC (rev 2669)
@@ -0,0 +1,19 @@
+# A GCD-based implementation of the sleeping barber problem:
+#   http://en.wikipedia.org/wiki/Sleeping_barber_problem
+#   http://www.madebysofa.com/#blog/the_sleeping_barber
+
+waiting_chairs = Dispatch::Queue.new('com.apple.waiting_chairs')
+semaphore = Dispatch::Semaphore.new(3)
+index = -1
+while true
+  index += 1
+  success = semaphore.wait(Dispatch::TIME_NOW)
+  if success != 0
+    puts "Customer turned away #{index}"
+    next
+  end
+  waiting_chairs.dispatch do
+    semaphore.signal
+    puts "Shave and a haircut #{index}"
+  end
+end
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20090928/50dc6a61/attachment.html>


More information about the macruby-changes mailing list