volatile

Java: Do all mutable variables need to be volatile when using locks?

Does the following variable, x, need to be volatile? Or does the manipulation within a utils.concurrent lock perform the same function as a synchronized block (ensuring it's written to memory, and not stored in cpu cache)? myMethod(){ myLock.lock(); x++; myLock.unlock(); } ...

C# memory model and non volatile variable initialized before the other thread creation.

Hi, I have a question related to the C# memory model and threads. I am not sure if the following code is correct without the volatile keyword. public class A { private int variableA = 0; public A() { variableA = 1; Thread B = new Thread(new ThreadStart(() => printA())).Start(); } private void printA() { System.C...

Does Java synchronized keyword flush the cache?

Java 5 and above only. Assume a multiprocessor shared-memory computer (you're probably using one right now). Here is a code for lazy initialization of a singleton: public final class MySingleton { private static MySingleton instance = null; private MySingleton() { } public static MySingleton getInstance() { if (instance == ...

Volatile keyword in Java - Clarification

I am really confused about what I read about the applications of volatile keyword in java. Is the following statement correct? "a write to a volatile field happens before every subsequent read of the same field" Ideally when should volatile keyword used? What is the difference between: class TestClass { private int x; synchro...

What kinds of optimizations does 'volatile' prevent in C++?

Hello, I was looking up the keyword volatile and what it's for, and the answer I got was pretty much: It's used to prevent the compiler from optimizing away code. There were some examples, such as when polling memory-mapped hardware: without volatile the polling loop would be removed as the compiler might recognize that the condit...

Side effect / Volatile / Copy Constructor / Destructor

With reference to the discussion here $3.7.1/2 - "If an object of static storage duration has initialization or a destructor with side effects, it shall not be eliminated even if it appears to be unused, except that a class object or its copy may be eliminated as specified in 12.8." $12.8/15- "When certain criteria are met, an implemen...

messed up using do_futex?

I'm getting a weird error. I implemented these two functions: int flag_and_sleep(volatile unsigned int *flag) { int res = 0; (*flag) = 1; res = syscall(__NR_futex, flag, FUTEX_WAIT, 1, NULL, NULL, 0); if(0 == res && (0 != (*flag))) die("0 == res && (0 != (*flag))"); return 0; } int wake_up_if_any(volatile ...

Is 'volatile' needed in this multi-threaded C++ code?

Hello, I've written a Windows program in C++ which at times uses two threads: one background thread for performing time-consuming work; and another thread for managing the graphical interface. This way the program is still responsive to the user, which is needed to be able to abort a certain operation. The threads communicate via a shar...

using volatile on atomic variables

Using volatile on a variable reduces the risk of memory consistency error (Please correct me if this reveals some holes in my understanding of any relevant concept). So in the following example even though the variable c1 is volatile, still the occurrence of memory constancy error results in c1 becoming 15 or sometimes 14 in the output ...

Where to use volatile?

I read about volatile keyword, but I don't know in what situations I should use it. When the memory (variable) is getting updated and process is not aware of that? In what cases should drivers use volatile variables? ...

C++ volatile keyword with global shared variable accessed by function

Hi, I have a multi-threaded C++ application. Now I know that for global shared variables, you should use volatile in some cases while checking the state of the variable or else the compiler could assume that the variable's value never changes (in that thread). What if, however, instead of checking the status of a variable I call a...

What does using volatile when using a java.util.concurrent.Concurrent* containers provide?

The question came up when I saw this code: private static volatile ConcurrentHashMap<String, String> cMap = null; static { cMap = new ConcurrentHashMap<String, String>(); } To me it looks like the volatile there is redundant as the container is ConcurrentHashMap which according the JavaDoc already has synchronized puts, DUH, the c...

C++ volatile and operator overloading

I have a class A that I overload its operator=. However it is required that I need to do something like this: volatile A x; A y; x = y; which raised an error while compiling error: no operator "=" matches these operands operand types are: volatile A = A If I removed volatile, it's compilable. Is there anyway to have this com...

What is the effect of InterlockedIncrement argument declared as volatile

InterlockedIncrement and other Interlocked operations declare their arguments as volatile. Why? What is the intention and effect of this? ...

volatile + object combination disallowed in C++?

I'm using an embedded compiler for the TI TMS320F28335, so I'm not sure if this is a general C++ problem (don't have a C++ compiler running on hand) or just my compiler. Putting the following code snippet in my code gives me a compile error: "build\main.cpp", line 61: error #317: the object has cv-qualifiers that are not compatible wit...

Does using "pointer to volatile" prevent compiler optimizations at all times?

Here's the problem: your program temporarily uses some sensitive data and wants to erase it when it's no longer needed. Using std::fill() on itself won't always help - the compiler might decide that the memory block is not accessed later, so erasing it is a waste of time and eliminate erasing code. User ybungalobill suggests using volat...

Java: volatile boolean vs AtomicBoolean

What does AtomicBoolean do that a volatile boolean cannot achieve? ...

volatile and double confusion

int x = 2; volatile int y = 2; const int z = x/y; int main(){ int x = 2 + 3; double d = 7 / 3; } I have three questions here: Firstly, can the compiler calculate the value of the 'z' at compile time to be 1 in this case? Secondly, I observed that the compiler does not generate assembly instructions for adding 2 and 3 to in...

Volatile classes in C++

I have a question concerning volatile keyword I can't seem to find an answer for. In my app I have data class that is shared as a state buffer between threads, and I need it to be updated regularly from multiple threads. Class looks like this: class CBuffer { //Constructor, destructor, Critical section initialization/destruction ...

Is volatile required here?

EDITED and refined my question after Johannes's valuable answer bool b = true; volatile bool vb = true; void f1() { } void f2() { b = false; } void(* volatile pf)() = &f1; //a volatile pointer to function int main() { //different threads start here, some of which may change pf while(b && vb) { pf(); } } So...