Is the volatile keyword necessary for an atom property?

My class has the property:

@property (readwrite, atomic) BOOL IsTrue; 

My understanding of the atomic qualifier is that @synthesize d getter / setter for a property guarantees serialization of access from different threads, i.e. if the producer stream is A set ting the value of the property, it will be allowed to complete the given operation before the consumer flows B and C are allowed to get the property value (as a digression, is atomic even necessary for a single byte / type POD?).

Does the volatile keyword provide extra data integrity?

 @property (readwrite, atomic) volatile BOOL IsTrue; 

What I'm doing is that there is a possibility that consumer threads will get obsolete values โ€‹โ€‹without using volatile ?

+4
source share
1 answer

is it likely that consumer flows will become obsolete without the use of volatiles?

No. From the clientโ€™s point of view, the property is just a paired getter / setter method. Therefore, any client needs to call objc_msgSend to set or get a value. Functional calls are synchronization points in C, so there is no way to get obsolete values โ€‹โ€‹(for example, with direct memory access, where volatile can be useful).

@ Synhesize'd accessors will take care of serializing access to the base value.

+8
source

Source: https://habr.com/ru/post/1444297/


All Articles