Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between 'atomic' and non-atomic? [duplicate]

Tags:

objective-c

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

like image 465
tek3 Avatar asked Jul 26 '10 04:07

tek3


People also ask

What does Atomic mean in IOS?

A Boolean value that indicates whether the entire operation fails when CloudKit can't update one or more records in a record zone.

What does non Atomic mean?

not atomic, not relating to or containing atoms. 2. logic. relating to a proposition that has a logical constant.

What is atomic and non atomic properties in Swift?

Swift has no such specifier. In Objective-C the implementation of an atomic property allows properties to be safely read and written from different threads. For nonatomic properties, the underlying pointer of a read value could be released when a new value is being written at the same time.

What is a non atomic attribute?

Non atomic properties has no guarantee regarding the returned value . It can be the correct value, a partially written value or even some garbage value. As most things that are not safe — this comes with enhanced speed of accessing this properties.


2 Answers

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:

@interface MyObject : NSObject 
{
    id myPropertyIVar;
}
-(id) myProperty;

@end

@implementation MyObject

-(id) myProperty
{
    return myPropertyIvar;
}

// other stuff

@end

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:

-(id) myProperty
{
    return [[myPropertyIvar retain] autorelease];
}

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:

  • the instance of MyObject is released and deallocated by another thread causing the ivar to be released and deallocated
  • myProperty is reassigned by another thread causing the old version to be released and deallocated

For this reason, all accesses to the property must be protected by a lock. The get accessor looks something like this.

-(id) myProperty
{
    // lock
    return [[myPropertyIvar retain] autorelease];
    // unlock
}

The set accessor is similarly protected and so is the release in -dealloc

like image 162
JeremyP Avatar answered Oct 30 '22 11:10

JeremyP


The Apple docs explain all this very well. To learn about properties, including their atomicity, read this page.

like image 45
mk12 Avatar answered Oct 30 '22 11:10

mk12