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
25
votes
2 answers

Is a write to a volatile a memory-barrier in Java

I recently heard in a talk that a write to a volatile triggers a memory-barrier for each variable that the thread has written to. Is that really correct? From the JLS, it seems that only the variable concerned gets flushed out, but not others. Does…
MKK
  • 670
  • 5
  • 13
24
votes
9 answers

Volatile and multithreading: is the following thread-safe?

Assume there are two threads running Thread1() and Thread2() respectively. The thread 1 just sets a global flag to tell thread 2 to quit and thread 2 periodically checks if it should quit. volatile bool is_terminate = false; void Thread1() { …
spockwang
  • 897
  • 1
  • 6
  • 15
24
votes
3 answers

Java: Make all fields either final or volatile?

If I have an object which is shared between threads, it seems to me that every field should be either final or volatile, with the following reasoning: if the field should be changed (point to another object, update the primitive value), then the…
Moritz
  • 1,085
  • 1
  • 8
  • 20
24
votes
3 answers

C++ - What does volatile represent when applied to a method?

If I have a C++ method declaration as follows: class A { public: double getPrice() volatile; }; What does volatile represent here? What could it be used for? You might be interested in this Dr Dobbs article by Andrei Alexandrescu. I was…
T33C
  • 4,341
  • 2
  • 20
  • 42
24
votes
3 answers

volatile struct = struct not possible, why?

struct FOO{ int a; int b; int c; }; volatile struct FOO foo; int main(void) { foo.a = 10; foo.b = 10; foo.c = 10; struct FOO test = foo; return 0; } This won't compile, because struct FOO test = foo; generates an…
makum
  • 243
  • 2
  • 7
24
votes
1 answer

CancellationTokenSource vs. volatile boolean

Are there any benefits for using a CancellationTokenSource over a volatile boolean field for signalling a Task to finish?
Yoav
  • 3,326
  • 3
  • 32
  • 73
24
votes
8 answers

May volatile be in user defined types to help writing thread-safe code

I know, it has been made quite clear in a couple of questions/answers before, that volatile is related to the visible state of the c++ memory model and not to multithreading. On the other hand, this article by Alexandrescu uses the volatile keyword…
David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
24
votes
3 answers

What does—or did—"volatile void function( ... )" do?

I've seen How many usage does "volatile" keyword have in C++ function, from grammar perspective? about use of the volatile keyword on functions, but there was no clear explanation of what Case 1 from that question did. Only a statement by one of…
EdwinW
  • 1,007
  • 2
  • 13
  • 32
24
votes
5 answers

Detailed semantics of volatile regarding timeliness of visibility

Consider a volatile int sharedVar. We know that the JLS gives us the following guarantees: every action of a writing thread w preceding its write of value i to sharedVar in program order happens-before the write action; the write of value i by w…
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
24
votes
3 answers

Volatile Violates its main job?

According to MSDN: The volatile keyword indicates that a field might be modified by multiple threads that are executing at the same time. Fields that are declared volatile are not subject to compiler optimizations that assume access by a…
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
23
votes
4 answers

In C, how do you declare the members of a structure as volatile?

How do you declare a particular member of a struct as volatile?
blak3r
  • 16,066
  • 16
  • 78
  • 98
23
votes
2 answers

Is this (volatile bool) always thread safe?

I'm wondering if this is completely thread-safe and whether or not the volatile keyword should be in place. using System.Threading; class Program { private static volatile bool _restart = true; private static void Main() { …
Aidiakapi
  • 6,034
  • 4
  • 33
  • 62
23
votes
5 answers

Volatile piggyback. Is this enough for visiblity?

This is about volatile piggyback. Purpose: I want to reach a lightweight vars visibilty. Consistency of a_b_c is not important. I have a bunch of vars and I don't want to make them all volatile. Is this code threadsafe? class A { public int a,…
temper
  • 395
  • 2
  • 9
23
votes
2 answers

Java: volatile implied order guarantees

My question is an extension to this one: Volatile guarantees and out-of-order execution To make it more concrete, let's say we have a simple class which can be in two states after it is initialized: class A { private /*volatile?*/ boolean…
Kovalsky
  • 339
  • 1
  • 2
  • 11
23
votes
5 answers

Volatile variable in Java

So I am reading this book titled Java Concurrency in Practice and I am stuck on this one explanation which I cannot seem to comprehend without an example. This is the quote: When thread A writes to a volatile variable and subsequently thread B …
denniss
  • 17,229
  • 26
  • 92
  • 141