[MacRuby-devel] Thread safety in apply example?

macruby at djc.net macruby at djc.net
Sat Jan 22 12:00:18 PST 2011



Taking
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Multithreading/ThreadSafetySummary/ThreadSafetySummary.html
as a sign, the answer appears to be:
neither of these can be safe, the latter even more so since the result is 
unordered.

If one is paranoid, not even sizing the array beforehand (a very good idea) is 
safe, unless someone can definitively state that no one (inc. MacRuby) will
be modifying/moving/otherwise manipulating this mutable object while you 
are....

a bit of a disappointment, since a) we know resizing occurs far less 
frequently then access (in general) b) making this all thread safe can easily 
be done at the Framework level c) most of the time, the behavior is exactly 
what we see here w.r.t. sizing (e.g. it grows UP, never down, and not up and 
down ... until GC kicks in and removes it all)

-Daniel



On Sat, 22 Jan 2011 06:52:04 -0800
  "Ernest N. Prabhakar, Ph.D." <prabhaka at apple.com> wrote:
> 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
> 
> _______________________________________________
> 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