3 ms·
> In a language like C, you can declare a variable to be volatile. When the program is compiled, this causes the compiler to emit memory barrier instructions I
by dragontamer 8y ago
> In a language like C, you can declare a variable to be volatile. When the program is compiled, this causes the compiler to emit memory barrier instructions
I'm going to guess you're a Java programmer. What you say is true in Java but not in C.
In C, volatile doesn't necessarily generate memory barriers. Volatile just means that the memory can't be stored in a register, the CPU will re-read the memory address each time. To generate memory barriers, you need to use the C11 atomics. A combination of "atomic_store" and "atomic_thread_fence" is needed. The C11 model has fences relative to a specific atomic operation.
See the newer header file #include<atomics.h> for more details.
- codebje 8y agoJava is "a language like C" if you squint? ;-) Thanks to you and others for the clarification on C's (lack of) behaviour!