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
800
votes
25 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,972
  • 4
  • 26
  • 32
777
votes
10 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
  • 32,451
  • 45
  • 138
  • 193
545
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
511
votes
2 answers

Why do we use the 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
  • 353,942
  • 115
  • 666
  • 851
364
votes
11 answers

When should the volatile keyword be used in C#?

Can anyone provide a good explanation of the volatile keyword in C#? Which problems does it solve and which it doesn't? In which cases will it save me the use of locking?
Doron Yaacoby
  • 9,412
  • 8
  • 48
  • 59
352
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
  • 9,141
  • 7
  • 32
  • 48
302
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
  • 12,368
  • 23
  • 64
  • 105
301
votes
12 answers

Volatile boolean vs AtomicBoolean

What does AtomicBoolean do that a volatile boolean cannot achieve?
JeffV
  • 52,985
  • 32
  • 103
  • 124
287
votes
9 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
  • 14,720
  • 22
  • 68
  • 93
275
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
  • 12,190
  • 11
  • 41
  • 49
187
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
  • 28,379
  • 9
  • 61
  • 93
170
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
  • 3,035
  • 7
  • 24
  • 27
168
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
  • 2,001
  • 2
  • 13
  • 11
139
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,369
  • 6
  • 27
  • 26
127
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
  • 27,718
  • 20
  • 89
  • 133
1
2 3
99 100