[MacRuby-devel] Thread safety in apply example?

Ernest N. Prabhakar, Ph.D. prabhaka at apple.com
Sat Jan 22 06:52:04 PST 2011


Hi Charles,

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?

My assumption was that doing a parallel assign:

result[i] = i*i
	
would be safe, since it always accessed a unique portion of memory, but doing a serial append:

result << i*i

would not.  But that may have been a mistake on my part, since the size (at least) needs to be updated. Anyone know better?

-- Ernie P.


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



More information about the MacRuby-devel mailing list