码迷,mamicode.com
首页 > 其他好文 > 详细

atomic vs. nonatomic

时间:2016-03-18 00:19:14      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

Declaring a property atomic makes compiler generate additional code that prevents concurrent access to the property. This additional code locks a semaphore, then gets or sets the property, and then unlock the semaphore. Compared to setting or getting a primitive value or a pointer, locking and unlocking a semaphore is expensive (although it is usually negligible if you consider the overall flow of your app). And also the atomic property doesn‘t mean thread safety. The property read by one thread for mutiple times may be different when it‘s modifing by another thread. The thread safety is garanteed by a more complicate mechanism.

Since most of your classes under iOS, especially the ones related to UI, will be used in a single-threaded environment, it is safe to drop atomic (i.e. write nonatomic, because properties are atomic by default): even though the operation is relatively inexpensive, you do not want to pay for things that you do not need.

 

参考:

http://stackoverflow.com/questions/588866/whats-the-difference-between-the-atomic-and-nonatomic-attributes

http://stackoverflow.com/questions/17571338/why-properties-are-always-said-to-be-made-nonatomic-in-objective-c

atomic vs. nonatomic

标签:

原文地址:http://www.cnblogs.com/drbbq/p/5289864.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!