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
0
votes
0 answers

JavaFX: BooleanProperty equivalent of volatile boolean

In normal Java (non-JavaFX) environment, it is common to use volatile boolean running to manage a multi-threaded task. If I want to wrap this into a JavaFX property equivalent, how do I go about doing this? I have planned to implement it this way,…
Jai
  • 7,616
  • 2
  • 17
  • 43
0
votes
4 answers

How to implement such use case by Java Concurrency Lock

In the test case below, the update() method is called outside of the class which in running for every hour by one thread only. But there have multiple threads calling getKey1() and getKey2() at the same time. So for this use case: The most important…
Jie
  • 167
  • 3
  • 14
0
votes
1 answer

Why reordering takes place with two volatile variables?

I'm trying to investigate the behaviour of reordering in java environment(using JDK 9-ea+170) and found one thing that i can't explain for myself, so i will be glad to hear some notes about it. Here's an example: public class Client { int x; …
the_kaba
  • 935
  • 2
  • 8
  • 24
0
votes
1 answer

Expected: end of statement error -- Coding a Volatile Formula

I have two volatile formulas that work fine when it is hard coded in Excel.. but this formula will be dynamic so I am inserting it in VBA and will loop later on. Worksheets("Interest Calculator").Range("E5").Formula =…
Rome876
  • 5
  • 2
0
votes
1 answer

Shared enum between multiple threads

I have an enumeration that is shared between multiple threads: public enum Action { Read, Write, None } Within a class I have a variable of Action type: public Action _action; This is a shared variable, that is, it is updated and read…
Ralph
  • 7,746
  • 14
  • 84
  • 190
0
votes
0 answers

C - volatile qualifier while lock is held

Do I need the volatile qualifier for variables only accessed while a lock is held? In this code, could removing the volatile qualifier from n possibly change the behavior when concurrent_foo is executed concurrently. #ifndef __GNUC__ #error…
Isabell Cowan
  • 83
  • 1
  • 9
0
votes
0 answers

Measure The speed write to variable between regluar one and volatile

I want to make a test, what is more efficient write to a regular variable or write to volatile variable. I wrote this code for the test private int v1= 42; private volatile int v2 = 43; public void measure(){ long startRegular =…
Ron Badur
  • 1,543
  • 2
  • 10
  • 25
0
votes
1 answer

volatile keyword : Need explanation for this program behaviour

understanding volatile keyword by this program: public class VolatileExample implements Runnable{ private volatile int vol; @Override public void run(){ vol=5; while(vol==5) { System.out.println("Inside run when vol is 5.…
0
votes
1 answer

Do I need to synchronize even if two threads don't read and write simultaneously?

In a Java program, suppose a variable is being written by one thread (say, T1) and the same variable is being read by another thread (say, T2) but not simultaneously. So, when I know T2 will read only after T1 has written (how that's done is a…
Learner
  • 413
  • 4
  • 17
0
votes
1 answer

When using inline PTX asm() instructions, what does 'volatile' do?

When we write inline PTX assembly in our generally C/C++ CUDA code, e.g.: __device__ __inline__ uint32_t bfind(uint32_t val) { uint32_t ret; asm ("bfind.u32 %0, %1;" : "=r"(ret): "r"(val)); return ret; } we can add the volatile keyword…
einpoklum
  • 86,754
  • 39
  • 223
  • 453
0
votes
1 answer

Non blocking variable updates

I'm going to implement non-blocking write to a variable via Volatile.Write. Should i use Volatile.Read for all consumers of this variable, or it is not necessary? What kind of impacts may occure if i read this variable as usual (without any kind of…
madmanul
  • 410
  • 4
  • 13
0
votes
4 answers

Should a pointer to stack variable be volatile?

I know that I should use the volatile keyword to tell the compiler not to optimize memory read\write to variables. I also know that in most cases it should only be used to talk to non-C++ memory. However, I would like to know if I have to use…
ZivS
  • 1,940
  • 1
  • 16
  • 44
0
votes
1 answer

Java volatile keyword behaviour changes due to remove local variable

From the link, it provide a demo of the java keyword 'volatile'. The demo code works fine. But I try to do a little modification. the behaviour is defferent. My code : public class VolatileTest4 { private static int MY_INT = 0; public…
gfan
  • 741
  • 1
  • 12
  • 25
0
votes
1 answer

volatile variables or volatile structure problems in C

Here is my header file (Header.h) #include #include void Process(void); and "Header.C" #include #include #include "Header.h" struct St{ unsigned long int volatile Var1; unsigned long int volatile…
0
votes
1 answer

How can compiler optimizations affect on a cache-unfriendly algorithm?

I've noticed an interesting behavior in the code of this question which is also comes from Agner Fog in Optimizing software in C++ and it reduces to how data is accessed and stored in the cache (cache associativity). The explanations is clear for…
Narek Atayan
  • 1,459
  • 13
  • 27
1 2 3
99
100