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
Thanx,问候,tek3
2 个解决方案
#1
8
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:
考虑一下get访问器的天真实现:
@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:
这很好,除非你在保留-myProperty的返回值之前释放MyObject的实例,否则很可能会释放返回的值。因此,像这样实现-myProperty更安全:
-(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
MyObject的实例被另一个线程释放并释放,导致ivar被释放和释放
myProperty由另一个线程重新分配,导致旧版本被释放和释放
For this reason, all accesses to the property must be protected by a lock. The get accessor looks something like this.
因此,必须通过锁保护对属性的所有访问。 get访问器看起来像这样。
-(id) myProperty
{
// lock
return [[myPropertyIvar retain] autorelease];
// unlock
}
The set accessor is similarly protected and so is the release in -dealloc
set访问器受到类似的保护,-dealloc中的版本也是如此
#2
2
The Apple docs explain all this very well. To learn about properties, including their atomicity, read this page.
Apple文档很好地解释了这一切。要了解属性,包括其原子性,请阅读此页面。
#1
8
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:
考虑一下get访问器的天真实现:
@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:
这很好,除非你在保留-myProperty的返回值之前释放MyObject的实例,否则很可能会释放返回的值。因此,像这样实现-myProperty更安全:
-(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
MyObject的实例被另一个线程释放并释放,导致ivar被释放和释放
myProperty由另一个线程重新分配,导致旧版本被释放和释放
For this reason, all accesses to the property must be protected by a lock. The get accessor looks something like this.
因此,必须通过锁保护对属性的所有访问。 get访问器看起来像这样。
-(id) myProperty
{
// lock
return [[myPropertyIvar retain] autorelease];
// unlock
}
The set accessor is similarly protected and so is the release in -dealloc
set访问器受到类似的保护,-dealloc中的版本也是如此
#2
2
The Apple docs explain all this very well. To learn about properties, including their atomicity, read this page.
Apple文档很好地解释了这一切。要了解属性,包括其原子性,请阅读此页面。