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
725
votes
9 answers

Volatile vs. Interlocked vs. lock

Let's say that a class has a public int counter field that is accessed by multiple threads. This int is only incremented or decremented. To increment this field, which approach should be used, and why? lock(this.locker)…
core
  • 30,054
  • 41
  • 131
  • 189
717
votes
24 answers

What is the volatile keyword useful for?

At work today, I came across the volatile keyword in Java. Not being very familiar with it, I found this explanation. Given the detail in which that article explains the keyword in question, do you ever use it or could you ever see a case in which…
Richard
  • 9,114
  • 4
  • 24
  • 31
460
votes
18 answers

Why is volatile needed in C?

Why is volatile needed in C? What is it used for? What will it do?
thomas
430
votes
2 answers

Why do we use volatile keyword?

Possible Duplicate: Why does volatile exist? I have never used it but I wonder why people use it? What does it exactly do? I searched the forum, I found it only C# or Java topics.
Nawaz
  • 327,095
  • 105
  • 629
  • 812
320
votes
7 answers

What is the difference between atomic / volatile / synchronized?

How do atomic / volatile / synchronized work internally? What is the difference between the following code blocks? Code 1 private int counter; public int getNextUniqueIndex() { return counter++; } Code 2 private AtomicInteger counter; public…
hardik
  • 7,975
  • 7
  • 26
  • 48
275
votes
8 answers

Volatile vs Static in Java

Is it correct to say that static means one copy of the value for all objects and volatile means one copy of the value for all threads? Anyway a static variable value is also going to be one value for all threads, then why should we go for volatile?
Jothi
  • 13,342
  • 20
  • 59
  • 92
264
votes
11 answers

Volatile boolean vs AtomicBoolean

What does AtomicBoolean do that a volatile boolean cannot achieve?
JeffV
  • 47,302
  • 31
  • 96
  • 120
264
votes
4 answers

Difference between volatile and synchronized in Java

I am wondering at the difference between declaring a variable as volatile and always accessing the variable in a synchronized(this) block in Java? According to this article http://www.javamex.com/tutorials/synchronization_volatile.shtml there is a…
Albus Dumbledore
  • 11,500
  • 22
  • 60
  • 102
243
votes
19 answers

Why does volatile exist?

What does the volatile keyword do? In C++ what problem does it solve? In my case, I have never knowingly needed it.
theschmitzer
  • 10,928
  • 11
  • 37
  • 48
170
votes
9 answers

Why is volatile not considered useful in multithreaded C or C++ programming?

As demonstrated in this answer I recently posted, I seem to be confused about the utility (or lack thereof) of volatile in multi-threaded programming contexts. My understanding is this: any time a variable may be changed outside the flow of control…
Michael Ekstrand
  • 26,173
  • 8
  • 54
  • 88
145
votes
5 answers

When to use volatile with multi threading?

If there are two threads accessing a global variable then many tutorials say make the variable volatile to prevent the compiler caching the variable in a register and it thus not getting updated correctly. However two threads both accessing a shared…
David Preston
  • 1,771
  • 2
  • 11
  • 10
141
votes
6 answers

Volatile Vs Atomic

I read somewhere below line. Java volatile keyword doesn't means atomic, its common misconception that after declaring volatile, ++ operation will be atomic, to make the operation atomic you still need to ensure exclusive access using …
Vaibhav
  • 2,585
  • 7
  • 21
  • 27
133
votes
8 answers

What is the "volatile" keyword used for?

I read some articles about the volatile keyword but I could not figure out its correct usage. Could you please tell me what it should be used for in C# and in Java?
Mircea
  • 3,089
  • 6
  • 23
  • 26
115
votes
4 answers

Is volatile expensive?

After reading The JSR-133 Cookbook for Compiler Writers about the implementation of volatile, especially section "Interactions with Atomic Instructions" I assume that reading a volatile variable without updating it needs a LoadLoad or a LoadStore…
Daniel
  • 25,883
  • 17
  • 87
  • 130
89
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
  • 31,028
  • 28
  • 106
  • 137
1
2 3
99 100