On Mon, Jan 24, 2011 at 11:23 AM, Joshua Ballanco <jball...@gmail.com> wrote:
> Arrays in MacRuby are not inherently thread safe. However,
> "Dispatch::Queue.new" creates a serial queue. So, there is an error in that
> each invocation of the block will execute serially. If, instead, the example
> had used "Dispatch::Queue.concurrent", then there would be a potential bug,
> but the easiest way to work around that is:
> gcdq = Dispatch::Queue.concurrent
> @result = []
> @result_q = Dispatch::Queue.new('@result_assignment_queue')
> gcdq.apply(5) { |i| res = i * i; @result_q.async { @result[i] = res } }
> p @result

Yes, forcing the updates to run serially rather than in parallel. The
poor-man's fork/join.

> There's also a mixin available in the Dispatch gem which abstracts away the
> need for a serial queue for assignments by monkey-patching assignments to
> all happen on a serial queue.

I'm curious what you mean by this. Can you point out the code? Is it
actually attempting to rewrite local variable or instance variable or
array modifications in code?

> For those wondering about the advantages to this technique, it's worth
> noting that GCD has a much lower overhead than more traditional methods of
> parallelism. That said, if the calculation for each iteration is small, this
> is not a useful technique. However, if the calculation for each iteration is
> long or (more likely) non-deterministic -- as in waiting for an external
> call or a remote connection -- then this can be a useful technique to
> continue doing useful work while letting iteration complete in the
> background.

I suppose it's time to explain why I'm looking into this stuff at all.

I'm building a JRuby-based mimic API of the MacRuby GCD API:
https://github.com/headius/jcd

I started out using java.util.concurrent Executors directly, and later
moved to using HawtDispatch, a libdispatch-like API. The latter has
done a reasonably good job of matching the necessary dispatch_*
functions in an OO API, and for the pieces that exist the wrapping job
has been trivial.

What I have now is a mostly-complete Queue and Object, but I'm still
figuring out how to map the other libdispatch primitives to what
HawtDispatch provides. The library currently is complete enough to run
a fork of ControlTower I made called ChaosBazaar:
http://github.com/headius/ChaosBazaar

There's no real reason I shouldn't be able to support most of the
MacRuby GCD APIs using just what's available on the JVM (i.e. no
native or OS dependencies), which would allow those APIs to be used on
any platform (and GCD client code that's not otherwise bound to
MacRuby to work on JRuby without modification). I think that would be
a good thing for concurrency in the Ruby world.

- Charlie
_______________________________________________
MacRuby-devel mailing list
MacRuby-devel@lists.macosforge.org
http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel

Reply via email to