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
43
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
  • 27,745
  • 21
  • 87
  • 120
39
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,347
  • 4
  • 17
  • 38
39
votes
6 answers

Can volatile variables be read multiple times between sequence points?

I'm making my own C compiler to try to learn as much details as possible about C. I'm now trying to understand exactly how volatile objects work. What is confusing is that, every read access in the code must strictly be executed (C11, 6.7.3p7): An…
Elzaidir
  • 891
  • 6
  • 13
39
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
4 answers

How to declare array elements volatile in Java?

Is there a way to declare array elements volatile in Java? I.e. volatile int[] a = new int[10]; declares the array reference volatile, but the array elements (e.g. a[1]) are still not volatile. So I'm looking for something like volatile int[] a =…
Joonas Pulakka
  • 36,252
  • 29
  • 106
  • 169
37
votes
6 answers

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…
softwarematter
  • 28,015
  • 64
  • 169
  • 263
37
votes
5 answers

c# - Volatile keyword usage vs lock

I've used volatile where I'm not sure it is necessary. I was pretty sure a lock would be overkill in my situation. Reading this thread (Eric Lippert comment) make me anxious on my usage of volatile: When should the volatile keyword be used in c# ? I…
Eric Ouellet
  • 10,996
  • 11
  • 84
  • 119
35
votes
6 answers

Is volatile bool for thread control considered wrong?

As a result of my answer to this question, I started reading about the keyword volatile and what the consensus is regarding it. I see there is a lot of information about it, some old which seems wrong now and a lot new which says it has almost no…
murrekatt
  • 5,961
  • 5
  • 39
  • 63
35
votes
6 answers

What does "volatile" mean in Java?

We use volatile in one of our projects to maintain the same copy of variable accessed by different threads. My question is whether it is alright to use volatile with static. The compiler does not give any errors but I don't understand the reason of…
jnivasreddy
  • 517
  • 2
  • 9
  • 16
34
votes
2 answers

Is the order of writes to separate members of a volatile struct guaranteed to be preserved?

Suppose I have a struct like this: volatile struct { int foo; int bar; } data; data.foo = 1; data.bar = 2; data.foo = 3; data.bar = 4; Are the assignments all guaranteed not to be reordered? For example without volatile, the compiler would clearly…
Ted Shaneyfelt
  • 745
  • 5
  • 14
34
votes
4 answers

While loop with empty body checking volatile ints - what does this mean?

I am looking at a C++ class which has the following lines: while( x > y ); return x - y; x and y are member variables of type volatile int. I do not understand this construct. I found the code stub here:…
Luca
  • 10,458
  • 24
  • 107
  • 234
34
votes
5 answers

AtomicInteger and volatile

I know volatile allows for visibility, AtomicInteger allows for atomicity. So if I use a volatile AtomicInteger, does it mean I don't have to use any more synchronization mechanisms? Eg. class A { private volatile AtomicInteger count; …
Achow
  • 8,600
  • 6
  • 39
  • 49
33
votes
2 answers

Should std::atomic be volatile?

I'm running a thread that runs until a flag is set. std::atomic stop(false); void f() { while(!stop.load(std::memory_order_{relaxed,acquire})) { do_the_job(); } } I wonder if the compiler can unroll loop like this (I don't want it to…
Inbae Jeong
  • 4,053
  • 25
  • 38
33
votes
2 answers

How does "Compare And Set" in AtomicInteger works

AtomicInteger works with two concepts : CAS and volatile variable. Using volatile variable insures that the current value will be visible to all threads and it will not be cached. But I am confused over CAS(compare AND set) concept which is…
Onki
  • 1,879
  • 6
  • 38
  • 58
33
votes
1 answer

When would I use const volatile, register volatile, static volatile in C++?

I am wondering about the different uses of the volatile keyword in combination with register, const and static keywords. I am not sure what are the effects, so I think: register volatile int T=10; Suggest the compiler to store T in a register and…
Avraam Mavridis
  • 8,698
  • 19
  • 79
  • 133