On 3/6/12 11:51 AM, Jan E. Schotsman wrote:
> Hello,
> 
> I have an array of progress values (number objects) for subprojects,
> from which I calculate the overall progress .
> The array is an atomic property of the project class.
> 
> Is it safe to access this array from multiple threads, using methods
> like objectAtIndex and replaceObjectAtIndex?

OK, you don't have an *array* you have a *mutable array*.  NSArray
itself is perfectly thread safe.

In short, no, NSMutableArray is NOT safe to use from multiple threads
out of the box
(https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Multithreading/ThreadSafetySummary/ThreadSafetySummary.html).

You would have to implement your own locking/synchronization if this is
an essential behavior.

The "atomic" keyword actually provides only very minimal thread safety:
it guarantees merely that other objects accessing your property won't
get an incorrect value if the property is in the process of being set.
Bill Bumgarner has an instructive blog post on this topic, explaining
why "atomic" is not synonymous with "thread safe":
http://www.friday.com/bbum/2008/01/13/objectivce-c-atomic-properties-threading-andor-custom-settergetter/

Since array mutations aren't going through the property setter, "atomic"
doesn't help here even to that meager degree.

Personally, I try to the maximum extent possible to make properties be
of immutable types for exactly this reason.  It allows you to centralize
management of locking/synchronization thus wipe out a whole category of
potential bugs.  It also allows for, e.g., easier KVO and generally
simpler code.

-- 
Conrad Shultz

Synthetiq Solutions
www.synthetiqsolutions.com
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to