[MacRuby-devel] Thread safety in apply example?

Laurent Sansonetti lsansonetti at apple.com
Wed Jan 26 14:45:56 PST 2011


Hi Charles!

Sorry for the late response.

As others have noted, in this snippet, #apply is called on a sequential queue (queues created by Queue.new are always sequential), therefore there shouldn't be any problem here. If the queue was concurrent, however, there would be a thread safety issue.

I think this rdoc snippet should be rewritten to avoid confusion.

I see that you're wrapping a GCD-like interface in JRuby, it's very cool! I assume you want your interface to be cross platform, but in the case of JRuby running on Mac OS X, maybe we can extract our code as a C extension, this way JRuby would use the "system" GCD. Maybe we can also work together on creating a good test/spec suite for the GCD interface, because it's currently lacking.

Laurent

On Jan 21, 2011, at 9:57 PM, Charles Oliver Nutter wrote:

> I'm curious about this example in Queue#apply's rdoc:
> 
> *     gcdq = Dispatch::Queue.new('doc')
> *     @result = []
> *     gcdq.apply(5) {|i| @result[i] = i*i }
> *     p @result  #=> [0, 1, 4, 9, 16, 25]
> 
> apply is said to issue the jobs in parallel, so this would be making
> concurrent updates to the @result array. Are simple arrays in MacRuby
> thread-safe?
> 
> - Charlie
> _______________________________________________
> MacRuby-devel mailing list
> MacRuby-devel at lists.macosforge.org
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-devel/attachments/20110126/2878948e/attachment-0001.html>


More information about the MacRuby-devel mailing list