[macruby-changes] [3517] MacRuby/trunk/sample-macruby/Scripts/gcd/dispatch_methods.rb
source_changes at macosforge.org
source_changes at macosforge.org
Fri Feb 12 11:24:11 PST 2010
Revision: 3517
http://trac.macosforge.org/projects/ruby/changeset/3517
Author: ernest.prabhakar at gmail.com
Date: 2010-02-12 11:24:10 -0800 (Fri, 12 Feb 2010)
Log Message:
-----------
Annoted dispatch sample with output
Modified Paths:
--------------
MacRuby/trunk/sample-macruby/Scripts/gcd/dispatch_methods.rb
Modified: MacRuby/trunk/sample-macruby/Scripts/gcd/dispatch_methods.rb
===================================================================
--- MacRuby/trunk/sample-macruby/Scripts/gcd/dispatch_methods.rb 2010-02-12 10:30:41 UTC (rev 3516)
+++ MacRuby/trunk/sample-macruby/Scripts/gcd/dispatch_methods.rb 2010-02-12 19:24:10 UTC (rev 3517)
@@ -3,43 +3,44 @@
puts "\n Use Dispatch.async to do stuff in the background"
Dispatch.async { p "Did this later" }
-sleep 0.1
+sleep 0.1 # => "Did this later"
puts "\n Use Dispatch.group to track when stuff completes"
g = Dispatch.group { p "Do this" }
Dispatch.group(g) { p "and that" }
-g.wait
+g.wait # => "Do this" "and that"
p "Done"
puts "\n Use Dispatch.fork to capture return values in a Future"
f = Dispatch.fork { 2+2 }
-p f.value
+p f.value # => 4
puts " - pass a block to return the value asynchronously"
f.value { |v| p "Returns #{v}" }
-sleep 0.1
+sleep 0.1 # => "Returns 4"
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
+p q # => Dispatch.enumerable.array.0x2000a6920.1266002369.9854
q.async { a << "change me" }
puts " - uses sync to block and flush queue"
-q.sync { p a }
+q.sync { p a } # => ["change me"]
puts "\n Use with a group for more complex dependencies, "
-q.async(g) { a << "more change" }
+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 {}
+q.sync {} # => ["change me", "more change", "complex calculation"]
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)
+b.size {|n| p "Size=#{n}"}
+sleep 0.1 # => "Size=1" (asynchronous return)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20100212/72022048/attachment.html>
More information about the macruby-changes
mailing list