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
0
votes
1 answer

Threads changing the same Variable wont synchronize

My problem is that the code should increment a 1000 times and then output it. But sometimes a isn't 1000 at the end. public class Counter extends Thread { private static Integer a = 0; public void run() { for (int i = 0; i <…
nordlad
  • 31
  • 1
  • 1
  • 4
0
votes
2 answers

Why is volatile keyword not needed for inter-thread visibility when one of the threads involved is the main() thread?

Consider the following program: import java.util.concurrent.TimeUnit; public class StopThread { public static boolean stopRequested; public static void main(String[] args) throws InterruptedException { Runnable task = new Runnable()…
theutonium.18
  • 483
  • 2
  • 7
0
votes
1 answer

UserWarning: volatile was removed and now has no essfet. Use 'with torch.no_grad():' insted

The version of PyTorch: 1.4.0+cu100 When running val_L= Variable(torch.from_numpy(val_L.copy()), volatile=True).cuda() occurred: UserWarning: volatile was removed and now has no essfet. Use 'with torch.no_grad():' insted. So, how should I modify…
hkzhang
  • 11
  • 1
  • 4
0
votes
0 answers

Two Threads and Terminal does not block even though struct termios c_cc[VMIN]=1

I got two threads (thrd_t): one main thread processing events and one child thread waiting for terminal input using fgetwc. #include // mtx_t, mtx_lock, mtx_unlock #include // fgetwc, fgetwc_unlocked FILE* f =…
Christian Heller
  • 140
  • 2
  • 10
0
votes
1 answer

multiThread modify a volatile variable, but this variable almost couldn't approach 10000

I've been thinking about it for almost the whole afternoon. Why this program its volatile variable almost couldn't approach 10000. Here is the code: public class TestModifyVolatile { volatile int count = 0; void m(){ count++; …
KevinWyen
  • 11
  • 1
0
votes
2 answers

Why can `asm volatile("" ::: "memory")` serve as a compiler barrier?

It is known that asm volatile ("" ::: "memory") can serve as a compiler barrier to prevent compiler from reordering assembly instructions across it. For example, it is mentioned in https://preshing.com/20120625/memory-ordering-at-compile-time/,…
zzzhhh
  • 319
  • 1
  • 8
0
votes
1 answer

UDF recalculates when data is entered in other occurrence of the UDF

Summary: all the occurrences of a UDF recalculate when one of them has a source changed. I have a fairly simple UDF (code below) that calculates the stableford score of a golf round based on a couple of variables. Now I find that the UDF seems to be…
Joost
  • 102
  • 8
0
votes
0 answers

Volatile bit-fields in C++

Let's say I have following definitions in C++ struct ControlReg { uint32_t reset_bit : 1; }; struct ConfigReg { uint32_t even_channel_value : 16; uint32_t odd_channel_value : 16; }; struct PeripheralRegs { volatile…
0
votes
2 answers

C++ struct mapped onto peripheral registers

Let's say I have following code in C++ running on a Zynq-7000 SoC struct ControlReg { uint32_t reset_bit : 1; }; struct ConfigReg { uint32_t even_channel_value : 16; uint32_t odd_channel_value : 16; }; struct…
0
votes
2 answers

volatile keyword in C, are all variables marked as volatile?

Sorry if I am asking a stupid question, but I can't find the answer due to clumsy search terms I guess If I declare three variables as follows volatile uint16_t a, b, c; Will all three variables be declared volatile? Or should I really not declare…
bas
  • 13,550
  • 20
  • 69
  • 146
0
votes
2 answers

Are variables updated by signal handlers optimized out when using RTEMS semaphore synchronization?

Let's say that the function isr_callback() is called on hardware interrupts. If my_function() sets the variable data to 0, and waits for tx_complete_semaphore, will the variable data be updated to 1 in my_function() when tx_complete_semaphore is…
LukesDiner
  • 145
  • 8
0
votes
3 answers

Visibility guarantees of atomic variables

Having read lots about volatile, atomic and visibility, one question remains. Following works across threads, "a" is visible all the time, when "b" is updated/read: int a; volatile int b; a = 1; b = 1; ... // different thread if (b == 1) // do…
0
votes
4 answers

After casting a volatile variable in c, is the variable still volatile?

Let's say that I have a static global variable a, that is cast to an int in a function call during init(). After init(), is a still volatile when used in the function my_function_2? static volatile int a; init(void) { …
LukesDiner
  • 145
  • 8
0
votes
0 answers

After GCC version upgrade, unexpected behavior of a reenterable function may be caused by GCC optimization

Upgraded the gcc version from 4.6.3 to 8.2.0. The following function doesn't work without the memory barrier. static u32 checksumPsedoHeader(u16 size, u32 src_addr, u32 dest_addr) { const u16 protocol_udp = 17; u32 checksum = 0; // Add…
0
votes
0 answers

Replace Stored procedure failed ,Volatile table doesn't exist

This procedure already exists in database but when I tried to replace after making changes It's throwing error volatile table doesn't exist. Can you pls help identify the issue ? This failed while migrating to QA environment. Statement 2: REPLACE…
1 2 3
99
100