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
28
votes
4 answers

Can volatile but unfenced reads yield indefinitely stale values? (on real hardware)

In answering this question a further question about the OP's situation came up that I was unsure about: it's mostly a processor architecture question, but with a knock-on question about the C++ 11 memory model as well. Basically, the OP's code was…
Stephen Lin
  • 5,470
  • 26
  • 48
28
votes
5 answers

How to force an unused memory read in C that won't be optimized away?

Microcontrollers often require a register to be read to clear certain status conditions. Is there a portable way in C to ensure that a read is not optimized away if the data is not used? Is it sufficient that the pointer to the memory mapped…
Judge Maygarden
  • 26,961
  • 9
  • 82
  • 99
28
votes
2 answers

Difference between Interlocked.Exchange and Volatile.Write?

What is the difference between Interlocked.Exchange and Volatile.Write? Both methods update value of some variable. Can someone summarize when to use each of them? Interlocked.Exchange Volatile.Write In particular I need to update double item of…
Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305
27
votes
2 answers

Can one volatile constexpr variable initialize another one in C++?

C++ standard allows constexpr volatile variables per defect report 1688, which was resolved in September 2013: The combination is intentionally permitted and could be used in some circumstances to force constant initialization. It looks though…
Fedor
  • 17,146
  • 13
  • 40
  • 131
27
votes
2 answers

Does empty synchronized(this){} have any meaning to memory visibility between threads?

I read this in an upvoted comment on StackOverflow: But if you want to be safe, you can add simple synchronized(this) {} at the end of you @PostConstruct [method] [note that variables were NOT volatile] I was thinking that happens-before is…
Piotr Müller
  • 5,323
  • 5
  • 55
  • 82
27
votes
2 answers

Why am I not provided with a default copy constructor from a volatile?

This code: class X { int member; }; volatile X a; X b = a; Fails with the error: prog.cpp:6:7: error: no matching function for call to ‘X::X(volatile X&)’ prog.cpp:6:7: note: candidates are: prog.cpp:1:7: note: X::X() prog.cpp:1:7: note: …
Eric
  • 95,302
  • 53
  • 242
  • 374
26
votes
1 answer

Why does modifying a field that is referenced by another variable lead to unexpected behavior?

I wrote this code that looked very simple to me at a first glance. It modifies a variable that is referenced by a reference variable and then returns the value of the reference. A simplified version that reproduces the odd behavior looks like…
Vadim Andronov
  • 261
  • 2
  • 5
26
votes
3 answers

How to properly access mapped memory without undefined behavior in C++

I've been trying to figure out how to access a mapped buffer from C++17 without invoking undefined behavior. For this example, I'll use a buffer returned by Vulkan's vkMapMemory. So, according to N4659 (the final C++17 working draft), section…
Socrates Zouras
  • 271
  • 2
  • 7
26
votes
4 answers

Java volatile array?

How do I make an array volatile? Because as I've come to understand, it's unsafe to make an array volatile?
Michael
  • 525
  • 2
  • 6
  • 6
26
votes
2 answers

What is the purpose of a volatile member function in C++?

What is the purpose of a volatile member function in C++?
user293666
  • 281
  • 3
  • 3
26
votes
2 answers

Should I mark object attributes as volatile if I init them in @PostConstruct in Spring Framework?

Suppose, that I do some initialization in Spring singleton bean @PostConstruct (simplified code): @Service class SomeService { public Data someData; // not final, not volatile public SomeService() { } @PostConstruct public void init() { …
Piotr Müller
  • 5,323
  • 5
  • 55
  • 82
26
votes
6 answers

Does Interlocked.CompareExchange use a memory barrier?

I'm reading Joe Duffy's post about Volatile reads and writes, and timeliness, and i'm trying to understand something about the last code sample in the post: while (Interlocked.CompareExchange(ref m_state, 1, 0) != 0) ; m_state = 0; while…
unknown
25
votes
3 answers

Is the compiler allowed to constant-fold a local volatile?

Consider this simple code: void g(); void foo() { volatile bool x = false; if (x) g(); } https://godbolt.org/z/I2kBY7 You can see that neither gcc nor clang optimize out the potential call to g. This is correct in my understanding:…
Max Langhof
  • 23,383
  • 5
  • 39
  • 72
25
votes
2 answers

An equivalent to Java volatile in Python

Does Python have the equivalent of the Java volatile concept? In Java there is a keyword volatile. As far as I know, when we use volatile while declaring a variable, any change to the value of that variable will be visible to all threads running at…
Pablo
  • 465
  • 1
  • 4
  • 14
25
votes
1 answer

std::is_trivially_copyable - Why are volatile scalar types not trivially copyable?

The current standards for C++17 (and I've observed similar wording for C++11) have very confusing wording for trivially copyable types. I first stumbled upon this problem with the following code (GCC 5.3.0): class TrivialClass…
TReed0803
  • 585
  • 3
  • 9