volatile vs. mutable in C++

A mutable field can be changed even in an object accessed through a const pointer or reference, or in a const object, so the compiler knows not to stash it in R/O memory. A volatile location is one that can be changed by code the compiler doesn’t know about (e.g. some kernel-level driver), so the … Read more

Is volatile expensive?

On Intel an un-contended volatile read is quite cheap. If we consider the following simple case: public static long l; public static void run() { if (l == -1) System.exit(-1); if (l == -2) System.exit(-1); } Using Java 7’s ability to print assembly code the run method looks something like: # {method} ‘run2’ ‘()V’ in … Read more

What is the “volatile” keyword used for?

Consider this example: int i = 5; System.out.println(i); The compiler may optimize this to just print 5, like this: System.out.println(5); However, if there is another thread which can change i, this is the wrong behaviour. If another thread changes i to be 6, the optimized version will still print 5. The volatile keyword prevents such … Read more

When to use volatile with multi threading?

Short & quick answer: volatile is (nearly) useless for platform-agnostic, multithreaded application programming. It does not provide any synchronization, it does not create memory fences, nor does it ensure the order of execution of operations. It does not make operations atomic. It does not make your code magically thread safe. volatile may be the single-most … Read more

Volatile Vs Atomic [duplicate]

The effect of the volatile keyword is approximately that each individual read or write operation on that variable is made atomically visible to all threads. Notably, however, an operation that requires more than one read/write — such as i++, which is equivalent to i = i + 1, which does one read and one write … Read more

Why is volatile not considered useful in multithreaded C or C++ programming?

The problem with volatile in a multithreaded context is that it doesn’t provide all the guarantees we need. It does have a few properties we need, but not all of them, so we can’t rely on volatile alone. However, the primitives we’d have to use for the remaining properties also provide the ones that volatile … Read more

Why does volatile exist?

volatile is needed if you are reading from a spot in memory that, say, a completely separate process/device/whatever may write to. I used to work with dual-port ram in a multiprocessor system in straight C. We used a hardware managed 16 bit value as a semaphore to know when the other guy was done. Essentially … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)