2014年10月16日 星期四

atomic vs. nonatomic

nonatomic :
  • does not enforce thread safety on the property, mainly for use when only one thread shall be used throughout a program.


atomic(default) : 我目前有找到幾種解釋,因為還沒整理完,所以先都貼上來。
  • enforces thread safety on the property, mainly for use when multiple threads shall be used throughout a program
  • More precisely, tomic properties do not ensure thread-safety; rather it ensures atomicity. If thread A and thread B are both writing, atomic ensures that the outcome will be a whole value, meaning either one or the other. Which one is undefined. Writing thread-safe code is not as simple as using atomic properties. See the "Synchronization" section of the Threading Programming Guide
  • atomic是Objc使用的一種線程保護技術,基本上來講,是防止在寫未完成的時候被另外一個線程讀取,造成數據錯誤。而這種機制是耗費系統資源的,所以在iPhone這種小型設備上,如果沒有使用多線程間的通訊編程,那麼 nonatomic 是一個非常好的選擇。
  • Atomicity has to do with how properties behave in a threaded environment. When you have more than one thread, it’s possible for the setter and the getter to be called at the same time. This means that the getter/setter can be interrupted by another operation, possibly resulting in corrupted data. Atomic properties lock the underlying object to prevent this from happening, guaranteeing that the get or set operation is working with a complete value. However, it’s important to understand that this is only one aspect of thread-safety—using atomic properties does not necessarily mean that your code is thread-safe.

整理來源:
http://stackoverflow.com/a/10753201/3295047
http://stackoverflow.com/questions/9162926/ios-property-declaration-clarification
http://rypress.com/tutorials/objective-c/properties.html