When Apple first introduced the concept of properties, there was a big argument about whether atomic
or nonatomic
should be the default and the atomic people won.
I think the reasoning is that in a threaded environment, unless the property is atomic, you can't guarantee that the pointer it returns is valid. A non atomic getter would be implemented something like this
-(MyObj*) someProperty
{
return someInstVar;
}
It's possible that some other thread could deallocate the object pointed to by someInstVar after the pointer has been placed in the register for returning but before the caller has had time to retain it. Even this is no good:
-(MyObj*) someProperty
{
return [[someInstVar retain] autorelease];
}
because some other thread could dealloc someInstVar just before the retain count is incremented.
The only way to safely return the pointer in a multithreaded environment is something like this:
-(MyObj*) someProperty
{
@synchronized(self)
{
return [[someInstVar retain] autorelease];
}
}
and also to synchronise the setter too.
However, the problem with that is that the lock is very expensive (in reality they used something much lighter than @synchronized but it's still expensive) so everybody was using nonatomic anyway and just making all the properties atomic doesn't confer thread safety in general so other techniques are required anyway and they tend to obviate the problem I discussed above.
There are plenty of people who think the wrong decision was made about what the default should be, but it can't be changed now for backwards compatibility. I find myself typing nonatomic
without even thinking now.