[macruby-changes] [3493] MacRuby/trunk

source_changes at macosforge.org source_changes at macosforge.org
Thu Feb 11 14:32:23 PST 2010


Revision: 3493
          http://trac.macosforge.org/projects/ruby/changeset/3493
Author:   ernest.prabhakar at gmail.com
Date:     2010-02-11 14:32:23 -0800 (Thu, 11 Feb 2010)
Log Message:
-----------
Added example showing new dispatch methods

Modified Paths:
--------------
    MacRuby/trunk/lib/dispatch/dispatch.rb

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

Modified: MacRuby/trunk/lib/dispatch/dispatch.rb
===================================================================
--- MacRuby/trunk/lib/dispatch/dispatch.rb	2010-02-11 22:32:06 UTC (rev 3492)
+++ MacRuby/trunk/lib/dispatch/dispatch.rb	2010-02-11 22:32:23 UTC (rev 3493)
@@ -5,7 +5,7 @@
   # Asynchronously run the +&block+  
   # on a concurrent queue of the given (optional) +priority+
   #
-  #   Dispatch.async {p "Do this later"}
+  #   Dispatch.async {p "Did this later"}
   # 
   def async(priority=nil, &block)
     Dispatch::Queue.concurrent(priority).async &block
@@ -27,8 +27,9 @@
   # returned for use with +wait+ or +notify+ --
   # on a concurrent queue of the given (optional) +priority+
   #
-  #   g = Dispatch.group {p "Did this"}
-  #   g.wait # => "Did this"
+  #   g = Dispatch.group {p "Do this"}
+  #   Dispatch.group(g) {p "and that"}
+  #   g.wait # => "Do this" "and that"
   # 
   def group(grp=nil, priority=nil, &block)
     grp ||= Dispatch::Group.new
@@ -53,7 +54,7 @@
   #
   #   a = Array.new
   #   q = Dispatch.queue_for(a)
-  #   q.async {a << Time.now }
+  #   q.async {a << 2 }
   #
   def queue_for(obj)
     Dispatch::Queue.new Dispatch.label_for(obj)
@@ -64,8 +65,8 @@
   #
   #   a = Dispatch.wrap(Array)
   #   a << Time.now # automatically serialized
-  #   a.size # => 1 (synchronously)
-  #   a.size {|n| p "Size=#{n}"} # => "Size=1" (asynchronously)
+  #   a.size # => 1 (synchronous return)
+  #   a.size {|n| p "Size=#{n}"} # => "Size=1" (asynchronous return)
   #
   def wrap(obj)
     Dispatch::Actor.new( (obj.is_a? Class) ? obj.new : obj)

Added: MacRuby/trunk/sample-macruby/Scripts/gcd/dispatch_methods.rb
===================================================================
--- MacRuby/trunk/sample-macruby/Scripts/gcd/dispatch_methods.rb	                        (rev 0)
+++ MacRuby/trunk/sample-macruby/Scripts/gcd/dispatch_methods.rb	2010-02-11 22:32:23 UTC (rev 3493)
@@ -0,0 +1,45 @@
+#!/usr/local/bin/macruby
+require 'dispatch'
+
+puts "\n Use Dispatch.async to do stuff in the background"
+Dispatch.async { p "Did this later" }
+sleep 0.1
+
+puts "\n Use Dispatch.group to track when stuff completes"
+g = Dispatch.group { p "Do this" }
+Dispatch.group(g) { p "and that" }
+g.wait
+p "Done"
+
+puts "\n Use Dispatch.fork to capture return values in a Future"
+f = Dispatch.fork {  2+2  }
+p f.value
+puts "  - pass a block to return the value asynchronously"
+f.value { |v| p "Returns #{v}" }
+sleep 0.1
+
+puts "\n Use Dispatch.queue_for to create a private serial queue"
+puts "  - synchronizes access to shared data structures"
+a = Array.new
+q = Dispatch.queue_for(a)
+puts "  - has a (mostly) unique name:"
+p q
+q.async { a << "change me"  }
+puts "  - uses sync to block and flush queue"
+q.sync { p a }
+
+puts "\n Use with a group for more complex dependencies, "
+q.async(g) { a << "more change"  }
+Dispatch.group(g) do 
+  tmp = "complex calculation"
+  q.async(g) { a << tmp }
+end
+puts "  - uses notify to execute block when done"
+g.notify(q) { p a }
+q.sync {}
+
+puts "\n Use Dispatch.wrap to serialize object using an Actor"
+b = Dispatch.wrap(Array)
+b << "safely change me"
+p b.size # => 1 (synchronous return)
+b.size {|n| p "Size=#{n}"} # => "Size=1" (asynchronous return)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20100211/ef41c930/attachment.html>


More information about the macruby-changes mailing list