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
94
votes
3 answers

Why is a point-to-volatile pointer, like "volatile int * p", useful?

volatile is to tell the compiler not to optimize the reference, so that every read/write does not use the value stored in register but does a real memory access. I can understand it is useful for some ordinary variable, but don't understand how…
Infinite
  • 3,198
  • 4
  • 27
  • 36
93
votes
6 answers

volatile vs. mutable in C++

I have a question about the difference between volatile and mutable. I noticed that both of the two means that it could be changed. What else? Are they the same thing? What's the difference? Where are they applicable? Why the two ideas are proposed?…
skydoor
  • 25,218
  • 52
  • 147
  • 201
91
votes
7 answers

When exactly do you use the volatile keyword in Java?

I have read "When to use 'volatile' in Java?" but I'm still confused. How do I know when I should mark a variable volatile? What if I get it wrong, either omitting a volatile on something that needs it or putting volatile on something that doesn't?…
Ricket
  • 33,368
  • 30
  • 112
  • 143
91
votes
8 answers

The need for volatile modifier in double checked locking in .NET

Multiple texts say that when implementing double-checked locking in .NET the field you are locking on should have volatile modifier applied. But why exactly? Considering the following example: public sealed class Singleton { private static…
Konstantin
  • 3,817
  • 4
  • 29
  • 39
90
votes
13 answers

Does the C++ volatile keyword introduce a memory fence?

I understand that volatile informs the compiler that the value may be changed, but in order to accomplish this functionality, does the compiler need to introduce a memory fence to make it work? From my understanding, the sequence of operations on…
Nathan Doromal
  • 3,437
  • 2
  • 24
  • 25
89
votes
6 answers

Illustrating usage of the volatile keyword in C#

I would like to code a little program which visually illustrates the behavior of the volatile keyword. Ideally, it should be a program which performs concurrent access to a non volatile static field and which gets incorrect behavior because of that.…
Romain Verdier
  • 12,833
  • 7
  • 57
  • 77
85
votes
2 answers

Volatile Struct Semantics

Is it sufficient to declare an instance of a structure-typed variable as volatile (if its fields are accessed in re-entrant code), or must one declare specific fields of the structure as volatile? Phrased differently, what are the semantic…
vicatcu
  • 5,407
  • 7
  • 41
  • 65
85
votes
13 answers

Simplest and understandable example of volatile keyword in Java

I'm reading about volatile keyword in Java and completely understand the theory part of it. But, what I'm searching for is, a good case example, which shows what would happen if variable wasn't volatile and if it were. Below code snippet doesn't…
tmgr
  • 1,187
  • 3
  • 14
  • 16
83
votes
6 answers

Is it allowed for a compiler to optimize away a local volatile variable?

Is the compiler allowed to optimize this (according to the C++17 standard): int fn() { volatile int x = 0; return x; } to this? int fn() { return 0; } If yes, why? If not, why not? Here's some thinking about this subject: current…
geza
  • 28,403
  • 6
  • 61
  • 135
74
votes
3 answers

Why is the volatile qualifier used through out std::atomic?

From what I've read from Herb Sutter and others you would think that volatile and concurrent programming were completely orthogonal concepts, at least as far as C/C++ are concerned. However, in GCC implementation all of std::atomic's member…
deft_code
  • 57,255
  • 29
  • 141
  • 224
70
votes
2 answers

How do I Understand Read Memory Barriers and Volatile

Some languages provide a volatile modifier that is described as performing a "read memory barrier" prior to reading the memory that backs a variable. A read memory barrier is commonly described as a way to ensure that the CPU has performed the reads…
Jason Kresowaty
  • 16,105
  • 9
  • 57
  • 84
69
votes
3 answers

Volatile properties in Kotlin?

How does one mark a var in Kotlin volatile? volatile public var tmpEndedAt: Long? = null gives me the error: "unresolved reference: volatile"
Andrew Cholakian
  • 4,392
  • 3
  • 26
  • 27
69
votes
4 answers

Why doesn't volatile in java 5+ ensure visibility from another thread?

According to: http://www.ibm.com/developerworks/library/j-jtp03304/ Under the new memory model, when thread A writes to a volatile variable V, and thread B reads from V, any variable values that were visible to A at the time that V was written are…
Oleg
  • 6,124
  • 2
  • 23
  • 40
67
votes
6 answers

Is a volatile int in Java thread-safe?

Is a volatile int in Java thread-safe? That is, can it be safely read from and written to without locking?
shawn
  • 4,063
  • 7
  • 37
  • 54
64
votes
4 answers

Working of __asm__ __volatile__ ("" : : : "memory")

What basically __asm__ __volatile__ () does and what is significance of "memory" for ARM architecture?
vnr1992
  • 761
  • 1
  • 6
  • 7