If I have an object which is shared between threads, it seems to me that every field should be either final or volatile, with the following reasoning:
When working with hardware it is sometimes required to perform a read from a specific register discarding the actual value (to clear some flags, for example). One way would be to explicitly read and discard the value such as:
I'm compiling a C++ program to run in a freestanding environment and the CPU I'm running on defines a 32-bit peripheral register to be available (edit: memory-mapped) at PERIPH_ADDRESS (aligned correctly, and not overlapping with any other C++ object, stack etc.).
After Googling a lot, I found multiple definitions for volatile keyword.Concept 1:Some Website says, it is thread safe, as thread acts on the main memory where volatile keyword is stored and modifies it without pulling it to thread stack space.
https://msdn.microsoft.com/en-us/magazine/jj883956.aspxConsider the polling loop pattern:In this case, the .NET 4.5 JIT compiler might rewrite the loop like this:
Where does the standard define that a volatile variable can change undetected?I've found two normative text which are about volatile:
Is the compiler allowed to optimize this (according to the C++17 standard):to this?If yes, why? If not, why not?
Is there ever any harm in making a variable "volatile" in Java if it doesn't actually need to be marked volatile? ... or is it just "unnecessary" as I have often read.
When you use the -O0 compiler flag in C, you tell the compiler to avoid any kind of optimization. When you define a variable as volatile, you tell the compiler to avoid optimizing that variable. Can we use the two approaches interchangeably? And if so what are the pros and...