Re: NSDictionary @synchronize to read multiple objects
Re: NSDictionary @synchronize to read multiple objects
- Subject: Re: NSDictionary @synchronize to read multiple objects
- From: Ken Thomases <email@hidden>
- Date: Tue, 01 Apr 2014 00:36:00 -0500
On Apr 1, 2014, at 12:25 AM, Jens Alfke wrote:
> On Mar 31, 2014, at 10:16 PM, Trygve Inda <email@hidden> wrote:
>
>> I need to be able to set a new myDict and not have it happen between the
>> reading of different values from the dictionary by other threads.
>
> Don’t expose the dictionary in mutable form. You can’t make that thread-safe unless all the clients voluntarily agree to do something like wrap their usage with @synchronized blocks using the dictionary as a parameter.
I didn't see mention of mutating the dictionary. It sounds like he's just using a setter to replace it (which also likely releases it, which makes it potentially unsafe).
I agree, though, that properties should almost never be of mutable type. But making them immutable doesn't, by itself, create thread-safety. If nothing else, the owner could still mutate it while other threads are reading it.
Regards,
Ken
_______________________________________________
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