0Day Forums
What's the difference between the atomic and nonatomic attributes? - Printable Version

+- 0Day Forums (https://0day.red)
+-- Forum: Coding (https://0day.red/Forum-Coding)
+--- Forum: Objective-C (https://0day.red/Forum-Objective-C)
+--- Thread: What's the difference between the atomic and nonatomic attributes? (/Thread-What-39-s-the-difference-between-the-atomic-and-nonatomic-attributes)

Pages: 1 2 3


What's the difference between the atomic and nonatomic attributes? - unshipwrecked546169 - 07-21-2023

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?


RE: What's the difference between the atomic and nonatomic attributes? - cavatina749 - 07-21-2023

The **default** is `atomic`, this means it does cost you performance whenever you use the property, but it is thread safe. What Objective-C does, is set a lock, so only the actual thread may access the variable, as long as the setter/getter is executed.

Example with MRC of a property with an ivar _internal:

[_internal lock]; //lock
id result = [[value retain] autorelease];
[_internal unlock];
return result;

So these last two are the same:

@property(atomic, retain) UITextField *userName;

@property(retain) UITextField *userName; // defaults to atomic


On the other hand does `nonatomic` add nothing to your code. So it is only thread safe if you code security mechanism yourself.

@property(nonatomic, retain) UITextField *userName;

*The keywords doesn't have to be written as first property attribute at all.*

**Don't forget, this doesn't mean that the property as a whole is thread-safe. Only the method call of the setter/getter is. But if you use a setter and after that a getter at the same time with 2 different threads, it could be broken too!**


RE: What's the difference between the atomic and nonatomic attributes? - weeiachj - 07-21-2023

I found a pretty well put explanation of atomic and non-atomic properties [here](

[To see links please register here]

). Here's some relevant text from the same:

> 'atomic' means it cannot be broken down.
In OS/programming terms an atomic function call is one that cannot be interrupted - the entire function must be executed, and not swapped out of the CPU by the OS's usual context switching until it's complete. Just in case you didn't know: since the CPU can only do one thing at a time, the OS rotates access to the CPU to all running processes in little time-slices, to give the *illusion* of multitasking. The CPU scheduler can (and does) interrupt a process at any point in its execution - even in mid function call. So for actions like updating shared counter variables where two processes could try to update the variable at the same time, they must be executed 'atomically', i.e., each update action has to finish in its entirety before any other process can be swapped onto the CPU.

> So I'd be guessing that atomic in this case means the attribute reader methods cannot be interrupted - in effect meaning that the variable(s) being read by the method cannot change their value half way through because some other thread/call/function gets swapped onto the CPU.

Because the `atomic` variables can not be interrupted, the value contained by them at any point is (thread-lock) guaranteed to be **uncorrupted**, although, ensuring this thread lock makes access to them slower. `non-atomic` variables, on the other hand, make no such guarantee but do offer the luxury of quicker access. To sum it up, go with `non-atomic` when you know your variables won't be accessed by multiple threads simultaneously and speed things up.


RE: What's the difference between the atomic and nonatomic attributes? - charlesbpzmqsc - 07-21-2023

**There is no such keyword "atomic"**

@property(atomic, retain) UITextField *userName;

We can use the above like

@property(retain) UITextField *userName;

See Stack Overflow question *https://stackoverflow.com/questions/8036604*.



RE: What's the difference between the atomic and nonatomic attributes? - elviaelvie164 - 07-21-2023

The best way to understand the difference is using the following example.

Suppose there is an atomic string property called "name", and if you call `[self setName:@"A"]` from thread A, call `[self setName:@"B"]` from thread B, and call `[self name]` from thread C, then all operations on different threads will be performed serially which means if one thread is executing a setter or getter, then other threads will wait.

This makes property "name" read/write safe, but if another thread, D, calls `[name release]` simultaneously then this operation might produce a crash because there is no setter/getter call involved here. Which means an object is read/write safe (ATOMIC), but not thread-safe as another threads can simultaneously send any type of messages to the object. The developer should ensure thread-safety for such objects.

If the property "name" was nonatomic, then all threads in above example - A,B, C and D will execute simultaneously producing any unpredictable result. In case of atomic, either one of A, B or C will execute first, but D can still execute in parallel.



RE: What's the difference between the atomic and nonatomic attributes? - componed700289 - 07-21-2023

### Atomic

- is the default behavior
- will ensure the present process is completed by the CPU, before another process accesses the variable
- is not fast, as it ensures the process is completed entirely

### Non-Atomic

- is NOT the default behavior
- faster (for synthesized code, that is, for variables created using @property and @synthesize)
- not thread-safe
- may result in unexpected behavior, when two different process access the same variable at the same time



RE: What's the difference between the atomic and nonatomic attributes? - elleneu - 07-21-2023

Atomic means only one thread accesses the variable (static type). Atomic is thread-safe, but it is slow.

Nonatomic means multiple threads access the variable (dynamic type). Nonatomic is thread-unsafe, but it is fast.



RE: What's the difference between the atomic and nonatomic attributes? - cheyanneyhhad - 07-21-2023

After reading so many articles, Stack Overflow posts and making demo applications to check variable property attributes, I decided to put all the attributes information together:

1. `atomic` // Default
2. `nonatomic`
3. `strong = retain` // Default
4. `weak = unsafe_unretained`
5. `retain`
6. `assign` // Default
7. `unsafe_unretained`
8. `copy`
9. `readonly`
10. `readwrite` // Default

In the article *[**Variable property attributes or modifiers in iOS**][1]* you can find all the above-mentioned attributes, and that will definitely help you.

1. **`atomic`**
- `atomic` means only one thread access the variable (static type).
- `atomic` is thread safe.
- But it is slow in performance
- `atomic` is the 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. <br/> <br/>

Example:

@property (retain) NSString *name;

@synthesize name;


2. **`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 the `nonatomic` keyword in the property attribute.
- It may result in unexpected behavior, when two different process (threads) access the same variable at the same time. <br/> <br/>

Example:

@property (nonatomic, retain) NSString *name;

@synthesize name;

[1]:

[To see links please register here]





RE: What's the difference between the atomic and nonatomic attributes? - Sirscarletlte - 07-21-2023

**Atomic** = thread safety

**Non-atomic** = No thread safety

Thread safety:
--------------

Instance variables are thread-safe if they behave correctly when accessed from multiple threads, regardless of the scheduling or interleaving of the execution of those threads by the runtime environment, and with no additional synchronization or other coordination on the part of the calling code.


In our context:
---------------

If a thread changes the value of the instance the changed value is available to all the threads, and only one thread can change the value at a time.

Where to use `atomic`:
--------------------

if the instance variable is gonna be accessed in a multithreaded environment.

Implication of `atomic`:
----------------------

Not as fast as `nonatomic` because `nonatomic` doesn't require any watchdog work on that from runtime .

Where to use `nonatomic`:
------------------------

If the instance variable is not gonna be changed by multiple threads you can use it. It improves the performance.


RE: What's the difference between the atomic and nonatomic attributes? - canvasback501 - 07-21-2023

If you are using your property in multi-threaded code then you would be able to see the difference between nonatomic and atomic attributes. Nonatomic is faster than atomic and atomic is thread-safe, not nonatomic.

Vijayendra Tripathi has already given an example for a multi-threaded environment.