The difference between atomic and non atomic properties remained a favourite question for interviewers. Today we will analyse these keywords in detail. These keyword define the characteristics of how these properties will behave once they will be accessed from multiple threads simultaneously.

atomic (Default)

As the word explains itself, a single thread will be able to access the property at a given time. To explain more, only one thread will be able to access getter/setter of a property. Other threads have to wait until first thread releases get/set method. Let’s say we have a property firstName which is atomic.

- Thread A => obj.firstName=@"A"
- Thread B => obj.firstName=@"B"
- Thread C => NSLog("First Name: %@",obj.firstName);

It will ensure that the value of property remains consistent throughout the lifecycle. By default all properties are atomic.

Example atomic property:

@property (strong,atomic) NSString *firstName;
OR
@property (strong) NSString *firstName;

Sample getter setter: This is how the methods of a property will look for an atomic property after

@synthesize firstName = _firstName;
-(NSString *)firstName{
    @synchronized (self) {
        return }
}
-(void)setFirstName:(NSString *)firstName{
    @synchronized (self) {
        if(_firstName != firstName){
            [_firstName release];
            _firstName = [firstName retain];
        }
    }
}

Note: the Objective-C 2.0 specification, mentions that locks are used internally, but it doesn’t specify exactly how. What you see above, is roughly what an atomic getter/setter would look like, but it might not be accurate.

Pros: Ensures that user gets a valid value and not some garbage

Cons: Slow, as it has code to ensure read write safety

nonatomic

For non atomic properties multiple threads can access the properties simultaneously, so there is always a risk of inconsistency between values

Example:

@property (strong,nonatomic) NSString *firstName;

Sample getter setter: This is how the methods of a properties will look for an atomic property

@synthesize firstName = _firstName;
-(NSString *)firstName{
    return _firstName;
}
-(void)setFirstName:(NSString *)firstName{
    if(_firstName != firstName){
        [_firstName release];
        _firstName = [firstName retain];
    }
}

Pros: Fast as compared to atomic properties, as there is no extra code to control access of multiple threads

Cons: You are not guaranteed whether or not you will receive a valid value

TODO

  • Thread safe vs Read/Write Safe
  • When is code created for properties i.e how & getter and setter code is created?
  • When to use atomic vs non-atomic some solid rules?
  • Detailed Pros & Cons
  • Multi-threading on Single CPU vs Multiple threads

Further Reading