nonatomic

iOS开发多线程篇---atomic nonatomic区别

倖福魔咒の 提交于 2020-04-20 07:46:09
atomic :默认是有该属性的,这个属性是为了保证程序在多线程情况下,编译器会自动生成一些互斥加锁代码,避免该变量的读写不同步问题。 nonatomic :如果该对象无需考虑多线程的情况,请加入这个属性,这样会让编译器少生成一些互斥加锁代码,可以提高效率。 atomic的意思就是setter/getter这个函数,是一个原语操作。如果有多个线程同时调用setter的话,不会出现某一个线程执行完setter全部语句之前,另一个线程开始执行setter情况,相当于函数头尾加了锁一样,可以保证数据的完整性。 nonatomic不保证setter/getter的原语行,所以你可能会取到不完整的东西。因此 ,在多线程的环境下原子操作是非常必要的,否则有可能会引起错误的结果。 比如setter函数里面改变两个成员变量,如果你用nonatomic的话,getter可能会取到只更改了其中一个变量时候的状态,这样取到的东西会有问题,就是不完整的。当然 如果不需要多线程支持的话,用nonatomic就够了,因为不涉及到线程锁的操作,所以它执行率相对快些。 下面是载录的网上一段加了atomic的例子: {lock} if (property != newValue) { [property release]; property = [newValue retain]; } {unlock} 可以看出来

Will atomic operations block other threads?

假如想象 提交于 2019-12-21 05:25:12
问题 I am trying to make "atomic vs non atomic" concept settled in my mind. My first problem is I could not find "real-life analogy" on that. Like customer/restaurant relationship over atomic operations or something similar. Also I would like to learn about how atomic operations places themselves in thread-safe programming. In this blog post; http://preshing.com/20130618/atomic-vs-non-atomic-operations/ it is mentioned as: An operation acting on shared memory is atomic if it completes in a single

Objective-C: Defaults to atomic for scalar properties?

蓝咒 提交于 2019-12-10 03:57:34
问题 A friend told me that the @property default for scalar properties (BOOL, NSInteger, etc.) is nonatomic. I.e., @property BOOL followVenmo; defaults to @property (nonatomic) BOOL followVenmo; But, I was always under the impression that the default is always atomic, scalar or not. Which is it? 回答1: Be careful with this "scalar" terminology. An NSString * property is also a pointer, exactly like the example you provided with a pointer to BOOL. From Apple docs: (The Objective-C Programming

Atomic access to non-atomic memory location in C++11 and OpenMP?

此生再无相见时 提交于 2019-12-08 21:45:54
问题 OpenMP, in contrast to C++11, works with atomicity from a perspective of memory operations, not variables. That allows, e.g., to use atomic reads/writes for integers being stored in a vector with unknown size at compile time: std::vector<int> v; // non-atomic access (e.g., in a sequential region): v.resize(n); ... v.push_back(i); ... // atomic access in a multi-threaded region: #pragma omp atomic write // seq_cst v[k] = ...; #pragma omp atomic read // seq_cst ... = v[k]; In C++11, this is not

Will atomic operations block other threads?

送分小仙女□ 提交于 2019-12-03 16:41:58
I am trying to make "atomic vs non atomic" concept settled in my mind. My first problem is I could not find "real-life analogy" on that. Like customer/restaurant relationship over atomic operations or something similar. Also I would like to learn about how atomic operations places themselves in thread-safe programming. In this blog post; http://preshing.com/20130618/atomic-vs-non-atomic-operations/ it is mentioned as: An operation acting on shared memory is atomic if it completes in a single step relative to other threads. When an atomic store is performed on a shared variable, no other thread

Objective-c properties for primitive types

感情迁移 提交于 2019-11-29 10:07:12
问题 In Objective-C Does it ever make sense to specify a property for a primitive type as nonatomic ? I'm wondering about the difference between these two properties: @property (nonatomic) BOOL myBool; @property BOOL myBool; 回答1: The answer is technically YES they're different, but practically NO they're not, unless you code your own accessors. Let me explain. For an object pointer property, say @property NSObject *foo there is a clear and important difference in the code that gets generated if

What&#39;s the difference between the atomic and nonatomic attributes?

删除回忆录丶 提交于 2019-11-25 22:16:53
问题 What do atomic and nonatomic mean in property declarations? @property(nonatomic, retain) UITextField *userName; @property(atomic, retain) UITextField *userName; @property(retain) UITextField *userName; What is the operational difference between these three? 回答1: The last two are identical; "atomic" is the default behavior ( note that it is not actually a keyword; it is specified only by the absence of nonatomic -- atomic was added as a keyword in recent versions of llvm/clang). Assuming that