Re: Accessing array in thread safe way
Re: Accessing array in thread safe way
- Subject: Re: Accessing array in thread safe way
- From: Kyle Sluder <email@hidden>
- Date: Thu, 08 Mar 2012 15:53:02 -0800
On Mar 8, 2012, at 2:15 PM, Quincey Morris wrote:
> On Mar 8, 2012, at 12:18 , CoGe - Tamas Nagy wrote:
>
>> I should subclass NSMutableArray because I had a project which used NSMutableArray (calls) before, but was not thread safe. Anyway, I can't really understand why it would be _really_ better to implement just accessors, but maybe you could explain it a bit more?
>
> Well, slap my head if I'm missing your intention, but I don't see how having an *atomically* safe NSMutableArray helps at all. As soon as anyone in any thread writes:
>
> for (NSInteger i = 0; i <= atomicallySafeArray.count; i++) {
> id object = [atomicallySafeArray objectAtIndex: i];
> ...
> }
>
> or any such construct that contains 2+ references to the array that might see a different state of the array over time, then the code is broken and not thread safe. This is also broken:
>
> for (id object in atomicallySafeArray)
> …
>
> for the separate but related reason that fast enumeration will fail if the array mutates.
>
> Also, implementing the accessors won't solve either of these problems.
>
> Thread safety does *not* come from atomicity. Conversely, in many cases atomicity has no value whatsoever -- because thread safety is what's really required.
And this is why atomic-by-default is a misfeature. Slower code that contributes to a false impression of thread-safety.
--Kyle Sluder
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden