[MacRuby-devel] [MacRuby] #476: GCD Groups should be a wrapper around dispatch, not its own invocation style

MacRuby ruby-noreply at macosforge.org
Wed Dec 2 17:37:26 PST 2009


#476: GCD Groups should be a wrapper around dispatch, not its own invocation
style
----------------------------------------+-----------------------------------
 Reporter:  ernest.prabhakar@…          |       Owner:  lsansonetti@…        
     Type:  enhancement                 |      Status:  new                  
 Priority:  blocker                     |   Milestone:  MacRuby 0.5          
Component:  MacRuby                     |    Keywords:  gcd                  
----------------------------------------+-----------------------------------
 Right now, GCD Groups are treated almost like a Queue, so developers call
 dispatch on them -- except that:
 - you need to specify a queue as a parameter -- or use (atypically) have
 it invisibly invoke the default queue
 - there's no way to specify synchronous dispatch

 This works well if you really just want async concurrency, but becomes
 awkward (and IMHO confusing) for anything else.

   g = Dispatch::Group.new
     g.dispatch {work_function(i)}}
     g.dispatch(q_a) {work_function(i)}}
     g.dispatch(q_b, false) {work_function(i)}}
   g.wait

 I don't know if it is possible, but what I'd prefer is for Groups to take
 a block *containing* multiple dispatch invocations, and auto-magically
 associate them with a group.

 So for example, you could do:

 g = Dispatch::Group.new

 g.wait do
     q_a.dispatch(true) {work_function(i)}
     q_b.dispatch(false) {work_function(i)}
 end

 In this case, the "group" is synchronous so it automatically does a wait
 until the "child" dispatches complete.  For async behavior, simply do:

 g.on_completion { puts "I'm all done" }
 g.notify do
     q_a.dispatch {work_function(i)}
     q_b.dispatch {work_function(i)}
 end

 If you want to add some invocations to the group but neither wait or
 notify yet, use a "shovel" to add them:

 g << { q_c.dispatch {work_function(i)} }

 As a bonus, one could provide a convenience class method for the
 synchronous case that avoids the need for any variable at all:

 Dispatch::Group.wait do
     q_a.dispatch(true) {work_function(i)}
     q_b.dispatch(false) {work_function(i)}
 end

 While this does require you to be more explicit in the concurrency async
 case, I think this API better reflects the full semantics of GCD groups.

 Of course, this presumes that queue dispatches would need to check if
 they're executing inside a group; I'm sure that must be possible, but I
 don't know what the performance implications might be.  Still, I wanted to
 raise it now before 0.5 is final and people start relying on the current
 API.

-- 
Ticket URL: <http://www.macruby.org/trac/ticket/476>
MacRuby <http://macruby.org/>



More information about the MacRuby-devel mailing list