2 ms·
The meaning of volatile keyword is not "do not optimize accesses to this variable". Rather, it's "this value may change at any time". Let's say you have a vari
by georgecmu 16y ago
The meaning of volatile keyword is not "do not optimize accesses to this variable". Rather, it's "this value may change at any time".
Let's say you have a variable that's used as a flag for communication between two threads. Protecting it with spin_locks inside a thread that checks the flag repeatedly inside a loop is not going to prevent compiler from optimizing that access away -- because as far as the compiler is concerned, value of that flag does not change inside that loop -- period.
It appears that the real recommendation of the article is to process shared data in special functions that access them by dereferencing a pointer. I don't see how that's more efficient/effective for simple cases like described above than declaring a volatile variable.