[MacRuby-devel] Thread safety in apply example?

Charles Oliver Nutter headius at headius.com
Sun Jan 23 14:48:49 PST 2011


On Sat, Jan 22, 2011 at 2:00 PM,  <macruby at djc.net> wrote:
> 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.

Yes, that's not too surprising I guess

> 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....

That's certainly not specified behavior, although I *think* it would
always be safe in JRuby and MRI (don't know about MacRuby).

> 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)

In JRuby we ship a nonstandard extension JRuby::Synchronized you can
include into any class (or object) to force all methods to be
synchronized. The example in that case would look like:

@result = []
@result.extend JRuby::Synchronized

Or even better, create a SyncArray subclass that includes it.

I'd like for there to be a standard way to do this across Ruby impls
(in particular, I'd like a standard set of thread-safe collections in
Ruby proper), but perhaps the JRuby solution will provide a starting
point for everyone else.

- Charlie


More information about the MacRuby-devel mailing list