Well, technically, there is no guarantee that the assignment to _object is atomic. Practically speaking it might be because there is probably a single instruction before which _object is null and after which it is pointing to a valid object. It's not a good idea to make assumptions, though, because bugs creep in. Imagine if this was compiled under Objective-C 2.0 and _object was a garbage collected object reference; A bunch more than simply copying a value is going on in that assignment.
Anyway, to be safe, you should also synchronize -getObject:
Another option that might be higher performance is to use the explicit atomic operations. I haven't used these on Mac, so I'm not sure exactly how to do this, but you can find info in the docs. However, if the overhead of synchronization is causing performance problems, its usually better to redesign things to require less synchronization than it is to optimizing the sync operations themselves. (Also, atomic operations can have significant overhead themselves on multiprocessor/core systems, anyway, so again, it's best to reduce the need for synchronization.)
64-bit vs. 32-bit shouldn't make a difference.
Anyway, to be safe, you should also synchronize -getObject:
Code:
- (id)object;
{
id theObj;
@synchronized(self)
{
theObj = _object;
}
return theObj;
}
Another option that might be higher performance is to use the explicit atomic operations. I haven't used these on Mac, so I'm not sure exactly how to do this, but you can find info in the docs. However, if the overhead of synchronization is causing performance problems, its usually better to redesign things to require less synchronization than it is to optimizing the sync operations themselves. (Also, atomic operations can have significant overhead themselves on multiprocessor/core systems, anyway, so again, it's best to reduce the need for synchronization.)
64-bit vs. 32-bit shouldn't make a difference.