I searched and found immutable are thread safe while mutable is not. This is fine.But i got misleading notes, blogs, answers about atomic vs non-atomic about thread safety, kindly give an explanation for the answer.
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 operation on different thread will be performed serially which means if one thread is executing 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.
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.
Yours comment on this will help us....
And my question is, "which is thread safe in cocoa, atomic or non-atomic?"