Search Your Question

Difference between the atomic and nonatomic attributes?

Ans : 

Atomic property give guarantee that valid value will be return. But valid does not mean that correct value.

This also not mean that atomic property are thread safe. Different threads can attempt to read and write value at same time. So one of two value will be return - value before change or changed value.

So atomic property is suffering from performance hit due to locking and unlocking before and after get or set value.

Non atomic property has no guarantee regarding correct value, a partially correct value or may be garbage value.
This is not thread safe this enhanced speed of access property.

Atomic property lock while setting value,

While non atomic property does not lock while setting value. 

2 comments:

  1. non atomic = is faster than atomic because UI Related all task work on main Thread so why we use atomic so Keep it non-atomic

    atomic = is thread safe bcz example

    i hava value A=0 and i have one function is work in incremental value and i have second function it's getting value of A so first function calling incremental value so first thread calling value A so value has been increase A=1 and than second function getting calling so finally value incremental value print A=1 that's the easy example but we use non atomic so in this case all threads are working together so first function call getting value so doesn't getting increment value we getting wrong value in non atomic all threat are working together
    and in atomic only one time one threat use first incremental threat use than atomic does not allow getting threads it's locked first threat first incremental threat work is completed than use other getting threat. that's why use say atomic is threat safe

    ReplyDelete

Thanks