Re: returning from within @synchronized results in warnings
Re: returning from within @synchronized results in warnings
- Subject: Re: returning from within @synchronized results in warnings
- From: Ken Thomases <email@hidden>
- Date: Wed, 12 Aug 2009 07:59:47 -0500
On Aug 12, 2009, at 7:26 AM, Paul Summermatter wrote:
I'm a reborn Objective-C'er, but so take my advice as probably not
the most experienced since synchronized was not even available the
last time I used Objective-C. That being said, I would have
expected to see something more like:
- (NSString *)name
{
NSString *retval;
@synchronized (self) {
retval = [[name retain] autorelease];
}
return retval;
}
The above will satisfy the compiler and eliminate the spurious
warning, but Cem's original code was just as correct and, IMHO,
acceptable style. It shouldn't provoke that warning. The problem is
that the compiler doesn't seem to understand that the @synchronized
block is unconditional. It's imagining that code might reach a point
after the @synchronized block and execution will fall out of the
function without returning any value.
Also, I thought if you used properties that all of this was done
properly for you under the covers. Unless you have a specific need
to define your own accessor, you might consider just using a property.
No, declared and synthesized properties will not synchronize on self.
If declared (or allowed to default to) atomic, then they use some
techniques to make sure that no thread calling the getter will get an
inconsistent value -- they will either see the value as it was before
a given setter or as it is after, but never some in-between
frankenstein value -- but atomic properties don't guarantee any
consistency of that property with respect to the rest of the object's
state. Of course, merely synchronizing on self doesn't guarantee that
either, but if the rest of the implementation also uses
synchronization properly, it can achieve that.
Regards,
Ken
_______________________________________________
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