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
63
votes
5 answers

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

I'm having a theoretical (non-deterministic, hard to test, never happened in practice) hardware issue reported by hardware vendor where double-word write to certain memory ranges may corrupt any future bus transfers. While I don't have any…
Andreas
  • 5,086
  • 3
  • 16
  • 36
63
votes
6 answers

Pointer declared as constant as well as volatile

While reading I came across this type of declaration and the following line - const volatile char *p=(const volatile char *) 0x30; The value of p is changed by external conditions only I don't get what are the external conditions . And also what…
ameyCU
  • 16,489
  • 2
  • 26
  • 41
60
votes
3 answers

Is there any point in using a volatile long?

I occasionally use a volatile instance variable in cases where I have two threads reading from / writing to it and don't want the overhead (or potential deadlock risk) of taking out a lock; for example a timer thread periodically updating an int ID…
Adamski
  • 54,009
  • 15
  • 113
  • 152
58
votes
5 answers

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

I've looked at the other volatile vs. Atomicxxxx questions in SO (including this one) and have read the description of java.util.current.atomic, and I am not quite satisfied with the nuances. If I'm trying to decide between using volatile boolean…
Jason S
  • 184,598
  • 164
  • 608
  • 970
54
votes
3 answers

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

I am still pretty new to the concept of threading, and try to understand more about it. Recently, I came across a blog post on What Volatile Means in Java by Jeremy Manson, where he writes: When one thread writes to a volatile variable, and…
Christian
  • 6,070
  • 11
  • 53
  • 103
53
votes
5 answers

why using volatile with synchronized block?

I saw some examples in java where they do synchronization on a block of code to change some variable while that variable was declared volatile originally .. I saw that in an example of singleton class where they declared the unique instance as…
52
votes
7 answers

Are mutex lock functions sufficient without volatile?

A coworker and I write software for a variety of platforms running on x86, x64, Itanium, PowerPC, and other 10 year old server CPUs. We just had a discussion about whether mutex functions such as pthread_mutex_lock() ... pthread_mutex_unlock() are…
David
  • 1,023
  • 1
  • 8
  • 16
52
votes
1 answer

Why is volatile deprecated in C++20?

According to cppreference, most uses of the volatile keyword are to be deprecated in C++20. What is the disadvantage of volatile? And what is the alternative solution when not using volatile?
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
48
votes
1 answer

How to cast away the volatile-ness?

How to cast away the volatile-ness? Which c++ style cast should I use?
pintu
  • 705
  • 2
  • 6
  • 8
46
votes
4 answers

Do non-static member variables in a C++ struct/class need to be marked as volatile to be treated as volatile in a member function?

class MyClass { int x, y; void foo() volatile { // do stuff with x // do stuff with y } }; Do I need to declare x and y as volatile or will be all member variables treated as volatile automatically? I want to make…
0xbadf00d
  • 17,405
  • 15
  • 67
  • 107
46
votes
5 answers

Why not volatile on System.Double and System.Long?

A question like mine has been asked, but mine is a bit different. The question is, "Why is the volatile keyword not allowed in C# on types System.Double and System.Int64, etc.?" On first blush, I answered my colleague, "Well, on a 32-bit machine,…
lmat - Reinstate Monica
  • 7,289
  • 6
  • 48
  • 62
46
votes
5 answers

What is the point of making the singleton instance volatile while using double lock?

private volatile static Singleton uniqueInstance In a singleton when using double lock method for synchronization why is the single instance declared as volatile ? Can I achieve the same functionality without declaring it as volatile ?
44
votes
4 answers

"A reference to a volatile field will not be treated as volatile" implications

The following code using System.Threading; class Test { volatile int counter = 0; public void Increment() { Interlocked.Increment(ref counter); } } Raises the following compiler warning: "A reference to a volatile field…
Jader Dias
  • 88,211
  • 155
  • 421
  • 625
44
votes
5 answers

Are volatile variable 'reads' as fast as normal reads?

I know that writing to a volatile variable flushes it from the memory of all the cpus, however I want to know if reads to a volatile variable are as fast as normal reads? Can volatile variables ever be placed in the cpu cache or is it always…
pdeva
  • 43,605
  • 46
  • 133
  • 171
43
votes
2 answers

What is the purpose of the "volatile" keyword appearing inside an array subscript?

While I was browsing cppreference, I saw a strange type array in function parameters like this: void f(double x[volatile], const double y[volatile]); So, What is the purpose of the volatile keyword appearing inside an array subscript? What does it…
msc
  • 33,420
  • 29
  • 119
  • 214