CP.etc
CP.etc: Etc. concurrency rules
CP.200: Use volatile only to talk to non-C++ memory
volatileis used to refer to objects that are shared with "non-C++" code or hardware that does not follow the C++ memory model.
const volatile long clock;
- This describes a register constantly updated by a clock circuit.
clockisvolatilebecause its value will change without any action from the C++ program that uses it. - For example, reading
clocktwice will often yield two different values, so the optimizer had better not optimize away the second read in this code:
long t1 = clock;
// ... no use of clock here ...
long t2 = clock;
-
clock is
constbecause the program should not try to write toclock. -
Note: Unless you are writing the lowest level code manipulating hardware directly, consider
volatilean esoteric feature that is best avoided. -
Example, usually C++ code receives
volatilememory that is owned elsewhere (hardware or another language):
int volatile* vi = get_hardware_memory_location();
// note: we get a pointer to someone else's memory here
// volatile says "treat this with extra respect"
- Sometimes C++ code allocates the
volatilememory and shares it with "elsewhere" (hardware or another language) by deliberately escaping a pointer:
static volatile long vl;
please_use_this(&vl); // escape a reference to this to "elsewhere" (not C++)
volatilelocal variables are nearly always wrong -- how can they be shared with other languages or hardware if they're ephemeral?- The same applies almost as strongly to member variables, for the same reason.
void f() {
volatile int i = 0; // bad, volatile local variable
// etc.
}
class My_type {
volatile int i = 0; // suspicious, volatile member variable
// etc.
};
- Note: In C++, unlike in some other languages,
volatilehas nothing to do with synchronization.
CP.201: ??? Signals
- UNIX signal handling .... Might be worth reminding how little is async-signal-safe, and how to communicate with a signal handler (best is probably "not at all")