Questions tagged [volatile]

Volatile is a qualifier used to define a data storage area (object, field, variable, parameter) that "can change on its own", thus disallowing some code generator optimizations. In some but not all languages that recognize this qualifier the access to such data is thread safe.

Volatile data are thread safe in

  • C#
  • Java (5 and above)
  • Scala (depending on VM version)

Compilers ensure such semantics by emiting memory barrier instructions in the required order.

Volatile data are not thread safe in some implementations of

  • C
  • C++
  • Java

In these languages, compilers are only required to refrain from particular code optimizations, especially those that would remove or reorder accesses to volatile data. This is still useful for some specialized purposes:

  • Access to memory mapped devices
  • Taming of atypical library constructs such as longjmp.
  • Some very careful thread synchronization protocols

(There is no relation between volatile data and non-volatile memory.)

References

1917 questions
32
votes
4 answers

How do I track down the cause of a StackOverflowException in .NET?

I get a StackOverflowException when I run the following code: private void MyButton_Click(object sender, EventArgs e) { MyButton_Click_Aux(); } private static volatile int reportCount; private static void MyButton_Click_Aux() { try { /*remove…
PRASHANT P
  • 1,527
  • 1
  • 16
  • 28
32
votes
5 answers

Using volatile keyword with mutable object

In Java, I understand that volatile keyword provides visibility to variables. The question is, if a variable is a reference to a mutable object, does volatile also provide visibility to the members inside that object? In the example below, does it…
Hongbo
  • 1,107
  • 2
  • 11
  • 18
32
votes
3 answers

C: Volatile Arrays in C

The volatile keyword is used in C to prevent the compiler performing certain optimizations, amongst other subtle changes, on a variable. For example; volatile int my_int = 0; creates an integer. In some situations it may prevent the following…
FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
32
votes
2 answers

Volatile in C++11

In C++11 standard the machine model changed from a single thread machine to a multi threaded machine. Does this mean that the typical static int x; void func() { x = 0; while (x == 0) {} } example of optimized out read will no longer happen in…
Šimon Tóth
  • 35,456
  • 20
  • 106
  • 151
31
votes
6 answers

The 'volatile' keyword in the C language

I read some tutorial about volatile in the C language, but I still can not figure it out. Some say the volatile tells the complier optimizer that operations involving this variable should not be optimized in certain ways. This means anytime the…
user707549
31
votes
4 answers

Is the 'volatile' keyword still broken in C#?

Joe Albahari has a great series on multithreading that's a must read and should be known by heart for anyone doing C# multithreading. In part 4 however he mentions the problems with volatile: Notice that applying volatile doesn’t prevent a write…
Drakarah
  • 2,244
  • 2
  • 23
  • 23
31
votes
1 answer

Is the volatile keyword required for fields accessed via a ReentrantLock?

My question refers to whether or not the use of a ReentrantLock guarantees visibility of a field in the same respect that the synchronized keyword provides. For example, in the following class A, the field sharedData does not need to be declared…
Matthew Blackford
  • 3,041
  • 1
  • 24
  • 28
30
votes
4 answers

Volatile vs VolatileRead/Write?

I can't find any example of VolatileRead/write (try...) but still: When should I use volatile vs VolatileRead? AFAIK the whole purpose of volatile is to create half fences so: For a READ operation, reads/writes (on other threads) which comes…
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
29
votes
3 answers

AtomicReferenceFieldUpdater - methods set, get, compareAndSet semantics

From the Java AtomicReferenceFieldUpdater docs: Note that the guarantees of the compareAndSet method in this class are weaker than in other atomic classes. Because this class cannot ensure that all uses of the field are appropriate for purposes…
axel22
  • 32,045
  • 9
  • 125
  • 137
29
votes
2 answers

Do I need volatile for variables of reference types, too?

We often use volatile to ensure that a condition variable can be visible to every Thread. I see the volatile fields are all primitive type in code so far. Does object field has this problem? For example: class a { public String str; public…
Hesey
  • 4,957
  • 6
  • 31
  • 31
29
votes
4 answers

Does accessing a declared non-volatile object through a volatile reference/pointer confer volatile rules upon said accesses?

This'll be a long one, as to contextualise it and provide as much info as I can, I must meander through various links and quotes - as is often the only way once we enter the C/C++ Standard Rabbit Hole. If you have better citations or any other…
underscore_d
  • 6,309
  • 3
  • 38
  • 64
29
votes
5 answers

Could the JIT collapse two volatile reads as one in certain expressions?

Suppose we have a volatile int a. One thread does while (true) { a = 1; a = 0; } and another thread does while (true) { System.out.println(a+a); } Now, would it be illegal for a JIT compiler to emit assembly corresponding to 2*a…
aioobe
  • 413,195
  • 112
  • 811
  • 826
28
votes
4 answers

If volatile is useless for threading, why do atomic operations require pointers to volatile data?

I've been reading from many sources that the volatile keyword is not helpful in multithreaded scenarios. However, this assertion is constantly challenged by atomic operation functions that accept volatile pointers. For instance, on Mac OS X, we have…
zneak
  • 134,922
  • 42
  • 253
  • 328
28
votes
8 answers

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

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…
gablin
  • 4,678
  • 6
  • 33
  • 47
28
votes
7 answers

What is the difference between a static global and a static volatile variable?

I have used a static global variable and a static volatile variable in file scope, both are updated by an ISR and a main loop and main loop checks the value of the variable. here during optimization neither the global variable nor the volatile…
Manoj Doubts
  • 13,579
  • 15
  • 42
  • 45