Is Atomic property thread safe

if we define a property in the following way:

@property (atomic) NSString *latestObject; Can we assume that the read write to that value is thread safe? i.e the value will be correct. Or it is better to write our own setter/getter with Locks?

When defining a property like this — where you’re using a class, like NSString, that has a mutable subclass — it’s vital that you mark the property as copy. Without that, the property isn’t safe regardless of threading O-:

Assuming that, a property marked as atomic is… well… atomic. That is, you can read or write it from any thread and you’ll always get consistent behaviour. Specifically, a reader will always get a value that’s either the current value or a value that was current at some point in the past.

Is that “thread safe”? It depends on your definition of that term. My experience is that folks come to concurrency from different backgrounds, and their understanding of the term thread safe is based on that experience.

My experience is that atomic properties are rarely useful. Consider this classic example:

@property (atomic, copy, nonnull) NSString * familyName;
@property (atomic, copy, nonnull) NSString * givenName;

Each property is ‘thread safe’ but that doesn’t mean the object as a whole is. Someone who reads both properties might receive inconsistent values.

Now, rarely useful isn’t the same as never useful. Sometimes an atomic property is the right tool for the job. But, in general, my experience is that it’s better to manage concurrency as a higher level, which is what we’re doing with the Swift concurrency model.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

ok so lets define couple of things.

"thread safe" - lets define it as safe to use without crashing

"correctness of the value" - basically what you mentioned, a consistent behavior between right and reads.

Let's keep the discussion only to one property. In that case can we assume its thread safe and correctness of the value?

In that case can we assume its thread safe and correctness of the value?

Sure.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Sorry but I still have few follow up question.

  1. With the above definitions can I assume that

@property (nonatomic , copy, nonnull) NSString * familyName;

Will also be thread safe (no crash) and the value correctness in multi thread environment. My assumption arise coz from first look I would assume that the read/write occurs to the underhand ivar and the object is immutable but I am not 100% confident if there are any additional steps in the synthesized getter/setter

  1. From what I can see Apple interpretation of thread safety is the consistency of the data across the whole objets i.e in your case is the consistency between the familyName and givenName . am I correct?
Is Atomic property thread safe
 
 
Q