property-level locking (was Re: Suppressing warning)
property-level locking (was Re: Suppressing warning)
- Subject: property-level locking (was Re: Suppressing warning)
- From: Chris Hanson <email@hidden>
- Date: Thu, 17 Apr 2008 14:36:13 -0700
On Apr 17, 2008, at 1:50 PM, Jeff LaMarche wrote: When creating a synchronized accessor, like this (simple example):
- (NSString *)foo { @synchronized(foo) { return foo; } }
I'd question why you're writing a synchronized accessor in the first place. Accessors are almost always the least correct place to introduce locking; you almost always want at least object-level granularity to your synchronization, if not object graph (or subgraph)-level locking. The reason for this is that accessor-level locking does nothing to enforce multiple-property invariants for your classes.
Consider a Person that has givenName, familyName, and fullName properties; the fullName is synthesized by returning the givenName and familyName. If one thread changes the givenName while another changes the familyName, and the properties are locked individually instead of against the whole object, then you can have a situation where fullName will return an inconsistent value.
This is why in Core Data, locking is implemented by having NSManagedObjectContext conform to NSLocking: It locks at the level of an object graph, rather than at the level of a particular object or one specific property of an individual object.
-- Chris
|
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden