简体   繁体   中英

Will a memory access to a dynamically allocated variable with attribute "volatile" cause cache misses for every access?

My understanding is that the "volatile" qualifier says compiler not to cache the variable and read it from main memory for each access. And I believe that processor can't get hold of memory without bringing it to cache, Please clarify Thanks

The C standard leaves it to each C implementation to define what constitutes access to a volatile object. C 2018 6.7.3 8 says:

… What constitutes an access to an object that has volatile-qualified type is implementation-defined.

In typical C implementations, accessing a volatile object requires a load or store instruction, so the processor does access memory whenever the C program accesses the object. Further, cache coherency is often provided by hardware, so that other threads or processes accessing the same memory see changes in a timely manner (that is, in some sequence consistent with program semantics), but details vary.

Since access is implementation-defined, a C implementation could define access to a volatile object to require some additional memory synchronization or could define it in such a way as to be useless, merely accessing whatever register the compiler has cached the value in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM