I have a confusion in @property declaration between atomic & nonatomic?
For example:
@property(nonatomic, retain) UITextField * countryName;
@property(atomic, retain) UITextField *countryName;
What is the operational difference between both statements?
Tarun Kumar
17-Nov-2015Here is the difference:
atomic
- atomic means only one thread access the variable(static type).
- atomic is thread safe but it is slow in performance
- atomic is default behavior
- atomic accessors in a non garbage collected environment will use a lock to ensure that another thread does not interfere with the correct setting/getting of the value.
- it is not actually a keyword.
ForExample:
@property(retain)NSString *title;
@synthesize title;
nonatomic
- nonatomic means multiple thread access the variable(dynamic type).
- nonatomic is thread unsafe.
- but it is fast in performance
- nonatomic is NOT default behavior, we need to add nonatomic keyword in property attribute.
- it may result in unexpected behavior, when two different process (threads) access the same variable at the same time.
ForExample:
@property(nonatomic,retain)NSString *title;
@synthesize title;