• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSDictionary @synchronize to read multiple objects
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSDictionary @synchronize to read multiple objects


  • Subject: Re: NSDictionary @synchronize to read multiple objects
  • From: Dave <email@hidden>
  • Date: Thu, 10 Apr 2014 11:20:47 +0100

Hi,

I was confused by this:

> It also doesn't make sense to synchronize on the myDict object.  That's what is in flux.  You should synchronize on "self", the object which owns and manages myDict.

Surely as long as every access to the dictionary is @synchronized to the same entity it’s fine?

The way I’d atcheive this is to make the NSMutableDictionary a private property:

@property (nonatomic,strong)    NSMutableDictionary*      pDictX;

 and define public methods:

-(void) removeObjectFromDictXWithKey:(NSString*) theKey;
-(void) addObject:(id) theObject toDictXWithKey:(NSString*) theKey;
-(id) getObectFromDictXWithKey:(NSString*) theKey;

The body of these methods would encapsulated with @synchronized :

 @synchronized (self. pDictX)
     {

// Code to Access self.pDictX

     }

If you wanted two dictionaries to work in this pay, just define another set for pDictY, etc.

Surely if you  do @synchronized(self) then access to pDictX will be blocked needlessly by access to pDictY?

Cheers
Dave

On 1 Apr 2014, at 14:45, Trygve Inda <email@hidden> wrote:

>> 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
>>
>>
> Yes, the property is Mutable in the host app, but after being sent to the
> helper app (which has multiple threads), it could just as easily be an
> NSDictioanary (non-mutable).
>
> I just need to be 100% certain that when the helper app threads read two
> values from the dictionary, that the dictionary does not change between (or
> during) these two reads.
>
> T.
>
>
>
>
>
> _______________________________________________
>
> 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


_______________________________________________

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


  • Follow-Ups:
    • Re: NSDictionary @synchronize to read multiple objects
      • From: Roland King <email@hidden>
References: 
 >Re: NSDictionary @synchronize to read multiple objects (From: Trygve Inda <email@hidden>)

  • Prev by Date: Re: button cell highlight/state/type
  • Next by Date: Re: NSButton/NSBox interaction
  • Previous by thread: Re: NSDictionary @synchronize to read multiple objects
  • Next by thread: Re: NSDictionary @synchronize to read multiple objects
  • Index(es):
    • Date
    • Thread