[MacRuby-devel] Feedback: Dispatch high-level wrappers

Ernest N. Prabhakar, Ph.D. prabhaka at apple.com
Fri Feb 12 10:20:15 PST 2010


Hi Jordan,

On Feb 10, 2010, at 12:30 AM, Jordan K. Hubbard wrote:

> P.S. Is it just me, or do other people have a really hard time turning specs into usage-case examples when they read them?

In answer to your plea, I've not only commented up the Dispatch module with examples:

http://svn.macosforge.org/repository/ruby/MacRuby/trunk/lib/dispatch/dispatch.rb

I've expanded them into a sample-macruby script:

http://svn.macosforge.org/repository/ruby/MacRuby/trunk/sample-macruby/Scripts/gcd/dispatch_methods.rb
(also below)

Note that this requires the latest nightly to work, as I redid the semantics of Dispatch.group.

Fairly minimal, but something to start with.  Does that help?

-- Ernie P.


#!/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)


More information about the MacRuby-devel mailing list