In Objective-C you have a distinction between atomic and nonatomic properties:
@property (nonatomic, strong) NSObject *nonatomicObject;
@property (atomic, strong) NSObject *atomicObject;
From my understanding you can read and write properties defined as atomic from multiple threads safely, while writing and accessing nonatomic properties or ivars from multiple threads at the same time can result in undefined behavior, including bad access errors.
So if you have a variable like this in Swift:
var object: NSObject
Can I read and write to this variable in parallel safely? (Without considering the actual meaning of doing this).
@atomic
or@nonatomic
. or just atomic by default. (Swift is so incomplete, we can't tell much now) - Bryan Chenatomic
is generally not considered sufficient for thread-safe interaction with a property, except for simple data types. For objects, one generally synchronizes access across threads using locks (e.g.,NSLock
or@synchronized
) or GCD queues (e.g., serial queue or concurrent queue with "reader-writer" pattern). - Robatomic
doesn't ensure thread-safety for objects; and (b) if one properly uses one of the aforementioned synchronization techniques to ensure thread-safety (amongst other things, preventing simultaneous read/writes), the atomic issue is moot. But we still need/want it for simple data types, whereatomic
has real value. Good question! - Rob