[ACCEPTED]-What does the property "Nonatomic" mean?-properties
Take a look at the Apple Docs.
Basically, if you say 18 nonatomic
, and you generate the accessors using @synthesize
, then 17 if multiple threads try to change/read the 16 property at once, badness can happen. You 15 can get partially-written values or over-released/retained 14 objects, which can easily lead to crashes. (This 13 is potentially a lot faster than an atomic 12 accessor, though.)
If you use the default 11 (which is atomic
; there used to be no keyword 10 for this, but there is now), then the @synthesize
d methods 9 use an object-level lock to ensure that 8 multiple reads/writes to a single property 7 are serialized. As the Apple docs point 6 out, this doesn't mean the whole object is thread-safe, but the individual property reads/writes 5 are.
Of course, if you implement your own 4 accessors rather than using @synthesize
, I think these 3 declarations do nothing except express your 2 intent as to whether the property is implemented 1 in a threadsafe manner.
After reading so many Articles and StackOverflow 8 posts, and having made demo apps to check 7 Variable property attributes, I decided 6 to put all the attributes information together
- atomic //default
- nonatomic
- strong=retain //default
- weak= unsafe_unretained
- retain
- assign //default
- unsafe_unretained
- copy
- readonly
- readwrite //default
so 5 below is the detailed article link where 4 you can find above mentioned all attributes, that 3 will definitely help you. Many thanks to 2 all the people who give best answers here!!
- atomic
- Atomic means only one thread access the variable (static type).
- Atomic is thread safe.
- But it is slow in performance.
- Atomic is default behavior.
- Atomic accessors in a non garbage-collected environment (i.e. when using retain/release/autorelease) will use a lock to ensure that another thread doesn't interfere with the correct setting/getting of the value.
- it is not actually a keyword.
Example 1 :
@property (retain) NSString *name;
@synthesize name;
- nonatomic
- Nonatomic means multiple thread access the variable (dynamic type).
- Nonatomic is thread unsafe.
- But it is fast in performance.
- Nonatomic is NOT default behavior; we need to add nonatomic keyword in property attribute.
- it may result in unexpected behavior, when two different process (threads) access the same variable at the same time.
Example:
@property (nonatomic, retain) NSString *name;
@synthesize name;
In addition to what's already been said 5 about threadsafeness, non-atomic properties 4 are faster than atomic accessors. It's not 3 something you usually need to worry about, but 2 keep it in mind. Core Data generated properties 1 are nonatomic partially for this reason.
In a multi-threaded program, an atomic operation 5 cannot be interrupted partially through, whereas 4 nonatomic operations can.
Therefore, you 3 should use mutexes (or something like that) if 2 you have a critical operation that is nonatomic 1 that you don't want interrupted.
If you specify "atomic", the generated access 2 functions have some extra code to guard 1 against simultaneous updates.
Usually atomic means that writes/reads to 1 the property happen as a single operation. Atomic_operation
You can able to get a handle of this stuffs 3 by reading the below article.
Threading Explained with the nonatomic's purpose
nonatomic 2 - Not Thread Safe
atomic - Thread Safe - This 1 is the default property attribute.
The "atomic” means that access to the property is 16 thread-safe. while the "nonatomic" is the opposite 15 of it. When you declare a property in Objective-C 14 the property are atomic by default so that 13 synthesized accessors provide robust access 12 to property in a multithreaded environment—that 11 is, the value returned from the getter or 10 set via the setter is always fully retrieved 9 or set regardless of what other threads 8 are executing concurrently. But if you declare 7 property as nonatomic like below
@property (nonatomic, retain) NSString *myString;
then it 6 means a synthesized accessor for an object 5 property simply returns the value directly. The 4 effect of the nonatomic attribute depends 3 on the environment. By default, synthesized 2 accessors are atomic. So nonatomic is considerably 1 faster than atomic.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.