ChatGPT解决这个技术问题 Extra ChatGPT

What does the property "Nonatomic" mean?

What does "nonatomic" mean in this code?

@property(nonatomic, retain) UITextField *theUsersName;

What is the difference between atomic and nonatomic?

Thanks

Sorry, forgot to specify this is in Objective-c (cocoa)
Please also read this link - stackoverflow.com/questions/588866/…
For anyone reading this, for 2014 it's important to realise a lot of this information is hugely out of date. There is, in a word, no reason ever to use nonatomic and it's essentially wrong to ever use it, for any reason. it's ancient history. Nonatomic means "thread unsafe mode" and is (in a word) now totally irrelevant. Some comments here stackoverflow.com/q/23977765/294884
I would disagree with the "non relevant" statement. If the value changes Rarely and as the example in Apple doc is last name. It changes once then using an atomic hint is a waste of resources involved since the checks are not needed.

J
Jesse Rusak

Take a look at the Apple Docs.

Basically, if you say nonatomic, and you generate the accessors using @synthesize, then if multiple threads try to change/read the property at once, badness can happen. You can get partially-written values or over-released/retained objects, which can easily lead to crashes. (This is potentially a lot faster than an atomic accessor, though.)

If you use the default (which is atomic; there used to be no keyword for this, but there is now), then the @synthesized methods use an object-level lock to ensure that multiple reads/writes to a single property are serialized. As the Apple docs point out, this doesn't mean the whole object is thread-safe, but the individual property reads/writes are.

Of course, if you implement your own accessors rather than using @synthesize, I think these declarations do nothing except express your intent as to whether the property is implemented in a threadsafe manner.


I like this answer - less confusing, simpler and more complete that those found elsewhere!
Really great one and less confusing (+1) but can you please tell that why non-atomic potentially lot faster than an atomic accessor ?
@Wish Non-atomic accessors can be faster because to prevent other threads from reading/writing at the same time, you need to hold a mutex or do some other low-level tricks which cost CPU time. If you're using a lock, you can also end up blocking on other threads, which takes time, too.
@Rob You might want to ask a new question about that.
@Pratik Sorry, that's not correct. The quote you show is saying exactly the same thing I did: that making a property atomic avoids crashes while accessing individual properties but doesn't ensure that the object as a whole (i.e. accessing separate properties like the first and last names) is going to do the right thing.
T
Todd Lehman

After reading so many Articles and StackOverflow posts, and having made demo apps to check Variable property attributes, I decided 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 below is the detailed article link where you can find above mentioned all attributes, that will definitely help you. Many thanks to all the people who give best answers here!!

Variable property attributes or Modifiers in iOS

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 :

@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;

M
Marc Charbonneau

In addition to what's already been said about threadsafeness, non-atomic properties are faster than atomic accessors. It's not something you usually need to worry about, but keep it in mind. Core Data generated properties are nonatomic partially for this reason.


j
joshdick

In a multi-threaded program, an atomic operation cannot be interrupted partially through, whereas nonatomic operations can.

Therefore, you should use mutexes (or something like that) if you have a critical operation that is nonatomic that you don't want interrupted.


P
Paul Tomblin

If you specify "atomic", the generated access functions have some extra code to guard against simultaneous updates.


N
NANNAV

Usually atomic means that writes/reads to the property happen as a single operation. Atomic_operation


E
Easwaramoorthy K

You can able to get a handle of this stuffs by reading the below article.

Threading Explained with the nonatomic's purpose

nonatomic - Not Thread Safe

atomic - Thread Safe - This is the default property attribute.


Are you sure it's not the opposite? Atomic properties are safe but nonatomic properties are not safe. Last time I checked it was like that :P
U
USM

The "atomic” means that access to the property is thread-safe. while the "nonatomic" is the opposite of it. When you declare a property in Objective-C the property are atomic by default so that synthesized accessors provide robust access to property in a multithreaded environment—that is, the value returned from the getter or set via the setter is always fully retrieved or set regardless of what other threads are executing concurrently. But if you declare property as nonatomic like below

@property (nonatomic, retain)  NSString *myString;

then it means a synthesized accessor for an object property simply returns the value directly. The effect of the nonatomic attribute depends on the environment. By default, synthesized accessors are atomic. So nonatomic is considerably faster than atomic.


佚名

One is for multi threads. One isnt


IMHO: In order to get any upvotes, you need to be more specific and spend some time on writing your answer.