[MacRuby-devel] locals & dispatch: is using a pointer the best practice?

Joshua Ballanco jballanc at gmail.com
Sat Oct 22 00:32:44 PDT 2011


On Saturday, October 22, 2011 at 1:35 AM, Michael Johnston wrote:
> When I need to get a queue-protected result into a local in code that is concurrent (so I can't use an ivar) is a pointer the best (only) way to get the result of a sync block? Assuming I don't want to factor out  a method object.
> 
> 
> ex:
> 
> result_p = Pointer.new(:id)
> some_queue.sync do
> result_p.assign(value_protected_by_queue)
> end
> result = result_p[0]
> 
> it's not very ruby-ish...

There's no restriction on not using ivars in block. Indeed, even locals are in scope in a block (and with a #sync dispatched block such as the one you provided, there aren't even any threading issues):

result = nil
Dispatch::Queue.concurrent.sync do
  result = true
end
p result #=> true 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-devel/attachments/20111022/94af35bc/attachment.html>


More information about the MacRuby-devel mailing list