objective c - Why atomic is not sufficient in thread safety for complex objects -
atomic keyword applied properties of class, thread safety.
usage of atomic thread safe primitive data types not complex objects (modal objects). why ?
thanks in advance kind reply!
update
if person class having properties name, age, address.
consider
@property(atomic, strong) person *adam;
how far object adam thread safe ?
this dupe of what's difference between atomic , nonatomic attributes? prior edit, though wasn't obvious.
since answer referred often, added following underscore thread safety having kind of transactional model reads , writes controlled such system in known, integral, state when read or write happens.
atomicity of single property cannot guarantee thread safety when multiple dependent properties in play.
consider:
@property(atomic, copy) nsstring *firstname; @property(atomic, copy) nsstring *lastname; @property(readonly, atomic, copy) nsstring *fullname; in case, thread renaming object calling setfirstname: , calling setlastname:. in meantime, thread b may call fullname in between thread a's 2 calls , receive new first name coupled old last name.
to address this, need transactional model. i.e. other kind of synchronization and/or exclusion allows 1 exclude access fullname while dependent properties being updated.
Comments
Post a Comment