Possible Duplicate:
Atomic vs nonatomic properties
I just want to know what is the differneve between theses two lines of code :
@property(nonatomic, retain) NSString *str;
and
@property(atomic, retain) NSString *str;
Thanx,
Regards,
tek3
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Atomic properties are necessary in a reference counted multi threaded environment in order to stop objects from disappearing before a thread has a chance to retain them.
Consider the naive implementation of a get accessor:
This is all fine except that if you release the instance of MyObject before retaining the returned value from -myProperty the returned value may well be deallocated. For this reason, it is safer to implement -myProperty like this:
This is now completely safe in a single threaded environment.
Unfortunately, in a multithreaded environment there is a race condition. If the thread is interrupted at any time before the retain has incremented the retain count, either of the following will cause you to receive a garbage pointer:
For this reason, all accesses to the property must be protected by a lock. The get accessor looks something like this.
The set accessor is similarly protected and so is the release in -dealloc