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

1732 questions
56
votes
4 answers

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

What basically __asm__ __volatile__ () does and what is significance of "memory" for ARM architecture?
vnr1992
  • 681
  • 1
  • 6
  • 7
51
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
  • 5,291
  • 9
  • 40
  • 85
48
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
  • 3,331
  • 2
  • 11
  • 21
47
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…
46
votes
5 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
  • 963
  • 6
  • 15
44
votes
4 answers

C++ volatile member functions

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
  • 14,584
  • 15
  • 60
  • 93
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
  • 30,333
  • 19
  • 96
  • 184
43
votes
4 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
  • 6,119
  • 4
  • 44
  • 55
42
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
  • 81,082
  • 147
  • 410
  • 611
42
votes
5 answers

Volatile guarantees and out-of-order execution

IMPORTANT EDIT I know about the "happens before" in the thread where the two assignments are happening my question is would it be possible for another thread to be reading "b" non-null while "a" is still null. So I know that if you're calling doIt()…
SyntaxT3rr0r
  • 26,196
  • 20
  • 81
  • 119
41
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
  • 635
  • 2
  • 6
  • 8
41
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
  • 36,445
  • 42
  • 122
  • 154
38
votes
7 answers

C volatile variables and Cache Memory

Cache is controlled by cache hardware transparently to processor, so if we use volatile variables in C program, how is it guaranteed that my program reads data each time from the actual memory address specified but not cache. My understanding is…
Microkernel
  • 1,309
  • 4
  • 16
  • 34
38
votes
3 answers

Java memory model: volatile variables and happens-before

I'd like to clarify how happens-before relation works with volatile variables. Let we have the following variables: public static int i, iDst, vDst; public static volatile int v; and thread A: i = 1; v = 2; and thread B: vDst = v; iDst = i; Are…
38
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 ?