Why is volatile deprecated in C++20?

There’s a good talk by the C++ committee language evolution chair on why. Brief summary, the places that volatile is being removed from didn’t have any well defined meaning in the standard and just caused confusion. Motivating (Ambiguous) Examples Volatile bit Fields should be specified by your hardware manual and/or compiler. Is += a single/atomic … Read more

Are mutex lock functions sufficient without volatile?

Simplest answer is volatile is not needed for multi-threading at all. The long answer is that sequence points like critical sections are platform dependent as is whatever threading solution you’re using so most of your thread safety is also platform dependent. C++0x has a concept of threads and thread safety but the current standard does … Read more

why using volatile with synchronized block?

Synchronization by itself would be enough in this case if the first check was within synchronized block (but it’s not and one thread might not see changes performed by another if the variable were not volatile). Volatile alone would not be enough because you need to perform more than one operation atomically. But beware! What … Read more

Happens-before relationships with volatile fields and synchronized blocks in Java – and their impact on non-volatile variables?

Yes, it is guaranteed that thread 2 will print “done” . Of course, that is if the write to b in Thread 1 actually happens before the read from b in Thread 2, rather than happening at the same time, or earlier! The heart of the reasoning here is the happens-before relationship. Multithreaded program executions … Read more

When is it preferable to use volatile boolean in Java rather than AtomicBoolean? [duplicate]

The main difference between AtomicBoolean and volatile from a practical point of view is that the compare-and-set operation isn’t atomic with volatile variables. volatile boolean b; void foo() { if( b ) { //Here another thread might have already changed the value of b to false b = false; } } But seeing as all … Read more

Is there any point in using a volatile long?

Not sure if I understand your question correctly, but the JLS 8.3.1.4. volatile Fields states: A field may be declared volatile, in which case the Java memory model ensures that all threads see a consistent value for the variable (ยง17.4). and, perhaps more importantly, JLS 17.7 Non-atomic Treatment of double and long : 17.7 Non-atomic … Read more

Is a C compiler allowed to coalesce sequential assignments to volatile variables?

No, the compiler is absolutely not allowed to optimize those two writes into a single double word write. It’s kind of hard to quote the standard since the part regarding optimizations and side effects is so fuzzily written. The relevant parts are found in C17 5.1.2.3: The semantic descriptions in this International Standard describe the … Read more

Working of __asm__ __volatile__ (“” : : : “memory”)

asm volatile(“” ::: “memory”); creates a compiler level memory barrier forcing optimizer to not re-order memory accesses across the barrier. For example, if you need to access some address in a specific order (probably because that memory area is actually backed by a different device rather than a memory) you need to be able tell … Read more

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