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.

Filter by
Sorted by
Tagged with
1 vote
1 answer
26 views

Java use synchronized and volatile when manipulating multiple properties and locking on a lock object

I need to manipulate multiple properties in Java while keeping them in sync. Is this code correct (from my understanding this is): class X { private final Object lockObject = new Object(); ...
Fexl's user avatar
  • 35
0 votes
1 answer
69 views

Pass non-volatile variable's address to a volatile object pointer

Suppose I have the following function: void atomic_add(volatile unsigned * loc, unsigned incr ); And I have want to pass a variable to the first argument of this function. unsigned a; atomic_add(&...
sfzhang's user avatar
  • 709
1 vote
2 answers
83 views

Is it mandatory to declare a variable volatile when I have a synchronized block to increment an int in Java?

I'm learning about concurrency in Java and I'm watching a YouTube video. It is an explanation about increment operation in a multi-threading environment. The solution for that could be a volatile ...
elvis's user avatar
  • 822
0 votes
0 answers
96 views

Does JMM guarantees that volatile overwrites with the same value will be eventually visible?

JMM have guarantees that when thread A writes to volatile variable V all other (non-volatile) writes become visible to thread B after it reads that V value. Does that guarantee holds in presence of ...
user3489275's user avatar
1 vote
1 answer
42 views

Inconsistency between Volatile and Interlocked

Below is the source code: public static class Volatile { // ... public static ulong Read(ref ulong location); public static long Read(ref long location); public static double Read(ref ...
user22155685's user avatar
0 votes
1 answer
62 views

arm gcc: store-store ordering without volatile?

I am trying to use a shared index to indicate that data has been written to a shared circular buffer. Is there an efficient way to do this on ARM (arm gcc 9.3.1 for cortex M4 with -O3) without using ...
personal_cloud's user avatar
2 votes
2 answers
82 views

Store ordering for larger-than-native-size type - C spec

Suppose you have a pointer to a larger-than-native-type integer, what do recent C specs say regarding the store ordering? E.g.: volatile uint64_t *test = (volatile uint64_t *)(addr); *test = 0; On a ...
Sparky McAnderson's user avatar
-1 votes
2 answers
96 views

How to cast volatile typedef struct to non-volatile typedef struct as argument of the function

I'm writing a C library for STM32 and ran into a problem. I have typedef struct: typedef struct foo { // some struct elements } foo; Volatile variable with foo type: volatile foo bar; And ...
t1m013y's user avatar
0 votes
0 answers
102 views

Is there a situation where a std::atomic<T> should be marked as volatile?

What would be a scenario where you would want to mark std::atomic<T> or std::atomic_flag as volatile (if there is one)? What about a std::atomic_flag that's used in an interrupt handler? Does ...
Panda's user avatar
  • 748
1 vote
3 answers
107 views

Formal understanding of volatile semantic

5.1.2.3 defines the following: In the abstract machine, all expressions are evaluated as specified by the semantics. An actual implementation need not evaluate part of an expression if it can deduce ...
Some Name's user avatar
  • 8,772
0 votes
1 answer
53 views

Clarification about conv.qual#1

conv.qual#1: A qualification-decomposition of a type T is a sequence of cvi and Pi such that T is “cv0 P0 cv1 P1 ⋯ cv_n-1 Pn-1 cvn U” for n ≥ 0, where each cvi is a set of cv-qualifiers ([basic.type....
Enlico's user avatar
  • 23.9k
0 votes
2 answers
46 views

OpenCL kernel communication using volatile memory

I'm trying to get two OpenCL kernels to communicate with eath other. A worker kernel runs a loop and a control kernel feeds it jobs and tells it when it's done. I'm using a volatile device buffer for ...
Stand with Gaza's user avatar
1 vote
2 answers
99 views

How to tell the compiler to throw errors for the variables that are not declared as "volatile" but used within an interrupt handler?

We need to declare the variables that are not meant to be enregistered or optimized as volatile if they are only modified within an external event (within an interrupt in embedded domain). uint8_t ...
ceremcem's user avatar
  • 3,970
0 votes
1 answer
61 views

JMM specification example

I've read 17.4.5 article of Jmm spec and I have a question about this example. Thread 1; Thread 2; B = 1; A = 2; r2 = A; r1 = B; Authors write that r2 and r1 can be equal to 0 because of ...
Роман Григорьев's user avatar
0 votes
2 answers
79 views

Clarification needed about behaviour of java volatile keyword

I have been reading about the 'volatile' keyword because our application is full of variables of this type. This was done a lot of years ago, and now reviewing it I want to confirm some points. As an ...
JBalaguero's user avatar
0 votes
0 answers
48 views

Do modern JIT compilers keep Program Order(PO) inside spinning loops?

The consensus around here seems to be that the famous example given in the "Java Concurrency in Practice" book: public class NoVisibility { private static boolean ready; private ...
Delark's user avatar
  • 1,170
0 votes
0 answers
73 views

prevent compiler optimisation for looped volatile variable check in thread

Below code prints less "end" than "start" and I believe it's due to pAStruct->valueInMemory getting optimised away; because when I quit the application "quit" is ...
user5588495's user avatar
1 vote
0 answers
63 views

Does a member variable need to be volatile if no concurrent writes occur?

If a member variable is updated by one thread and later read (not updated) by other treads from a thread pool, does that member variable needs to be declared as volatile? Made-up code to illustrate ...
SebastianBrandt's user avatar
0 votes
0 answers
15 views

Is it necessary to make ThreadPoolExecutor as Volatile field?

I want to create a unique thread pool within the app. Is the following "volatile" mandatory? private volatile ThreadPoolExecutor cronTaskExecutor I am not sure about the ThreadPoolExecutor ...
Eric's user avatar
  • 1
1 vote
3 answers
122 views

Is gcc optimizing out my wait code, despite marking it volatile?

On a STM32 ARM Cortex M4, I have a simple bare metal blinky which strobes a LED on. It works fine when I use a spin wait: typedef volatile uint32_t vuint32_t; #define SET(addr, bits) (*((vuint32_t*) (...
SRobertJames's user avatar
  • 8,561
0 votes
0 answers
75 views

C# volatile usage for struct fields accessed from different threads

I am writing a library with task-like types for handling async/await code, to get a better understanding of how Tasks work in .NET under the hood. What bothers me is the situation when multiple ...
MrDyrektor's user avatar
0 votes
1 answer
86 views

Unable to Handle MISRA error 13.2 using a volatile pointer

I am not sure how to fix the fallowing MISRA error 13.2, The value of an expression and its persistent side effects shall be the same under all permitted evaluation orders. I know the error is flag ...
Jee's user avatar
  • 11
1 vote
3 answers
108 views

Explanation about the volatile keyword in C

Can someone give me an explanation about the use of the second volatile keyword in this line volatile unsigned int * volatile buffer; I know that the volatile keyword is usually used to indicate to ...
Jee's user avatar
  • 11
1 vote
1 answer
102 views

Use of volatile in circular buffers with interrupts

I am struggling with the correct use of the volatile keyword when implementing a circular buffer. The buffer is written in an ISR and read in the main program. I am running a bare-metal ...
makas005's user avatar
0 votes
0 answers
6 views

Having a UDF automatically update based on an update to an array it is searching through, WITHOUT using application.volatile

First time poster. I've created a UDF that does a search of an array for key words in cells in a particular mxn array, and then returns values in adjacent cells next to within that same array. Code ...
Joe Smith's user avatar
1 vote
1 answer
55 views

Understanding the working of volatile keyword

class OrderingTest { var x = 0 var y = 0 fun test() { thread { x = 1 y = 1 } thread { val a = y val b = x println(&...
cheems's user avatar
  • 206
0 votes
1 answer
43 views

How can we execute Java threads in a sequence?

Here is a scenario, when the output I am expecting from below code is First Function Second Function Main Execution Completed! But the problem here is the run method that prints "Second Function&...
Curious Coder's user avatar
0 votes
2 answers
65 views

Java singleton lazy initialization. Volatile vs synchronized method

Why do we need to add volatile to field to prevent invalid data retrieval? Can't we do the same thing by adding synchronized to method declaration instead of to a block of code? public class ...
kaka's user avatar
  • 625
0 votes
0 answers
11 views

Allocating variable at specific memory region at specific address in single header file

I am developing a bare-metal dual-core application in which I have an application running on Core 0 (a lwIP application) and another one running on Core 1. I also have some shared flags to be read by ...
Gianfranco Di Domenico's user avatar
0 votes
0 answers
41 views

C11: Use volatile for simple communication between threads

I'm using C (more exactly: C11 with gcc) for developing some low-latency software for x64 (more exactly: Intel CPUs only). I don't care about portability or any other architecture in general. I know ...
Kevin Meier's user avatar
  • 2,363
4 votes
1 answer
118 views

Using `volatile` has unexpected results

I am puzzled by following difference discovered by trying to replicate different bug. This program works as expected: #include <memory> #include <string> #include <iostream> int ...
Roman Pavelka's user avatar
0 votes
0 answers
28 views

Save the result of a CTE table into a volatile table

I want to save the data from a CTE (Common Table Expression) into a volatile table in Teradata. In other words, I need to make the following simple syntax work (created as an example): CREATE VOLATILE ...
Mario Bastos's user avatar
0 votes
1 answer
98 views

Which cpus have explicit cache flush assembly instructions?

As a followup to this question, I'd like to know which real-world cpus, if any, have instructions for explicitly flushing the cache/writing to the main memory. And under what circumstance are these ...
Malt's user avatar
  • 29.2k
3 votes
1 answer
130 views

Visibility of volatile writes in C#

According to section 14.5.4 of the C# language spec (ECMA 334, 6th Edition), volatile fields are all about preventing reordering: 14.5.4 Volatile fields When a field_declaration includes a volatile ...
Malt's user avatar
  • 29.2k
1 vote
1 answer
124 views

Why does adding a volatile qualifier to a variable not prevent instruction reordering?

I have a simple C++ code snippet as shown below: int A; int B; void foo() { A = B + 1; // asm volatile("" ::: "memory"); B = 0; } When I compile this code, the ...
tang's user avatar
  • 17
0 votes
0 answers
43 views

Why are the output of these two java programs different

import java.util.concurrent.TimeUnit; public class VolatileDemo1 { private static Object stop; public static void main(String[] args) throws InterruptedException { Thread t = new ...
chengshun fu's user avatar
1 vote
2 answers
86 views

Java volatile and happens-before

There is what java spec says about it: https://docs.oracle.com/javase/specs/jls/se8/html/jls-17.html#jls-17.4.5 "A write to a volatile field (§8.3.1.4) happens-before every subsequent read of ...
RedMurloc's user avatar
  • 117
1 vote
1 answer
33 views

Generic Volatile.Read to Thread.VolatileRead conversion

I need to get this code from .NET 4.5+ working on .NET 4.0. The generic Volatile.Read isn't available in that version. I'm trying to use the Thread.VolatileRead to get this working. I need to know if ...
Rex NFX's user avatar
  • 443
0 votes
2 answers
59 views

Is volatile enough to check on a collection/map?

Let's suppose i have a collection or a map in a multi-threaded enviroment , and i have to execute certain operations based on if a value is in the Data Structure (the operations have nothing to do ...
Dario Franzese's user avatar
0 votes
1 answer
80 views

Shared-memory through shm_open and mmap: how do I avoid thread or process local caches when reading from my shared-memory?

I'm using shm_open + mmap to write a consumer and a producer that exchange data through shared-memory. The consumer is in one process and the producer is in another process. All good, but how can I be ...
ThreadFrank's user avatar
13 votes
2 answers
790 views

Dereference a pointer to volatile structure in C++

I have a pointer to some volatile memory that I am trying to dereference and copy to a unqualified copy of that structure (and vise-versa). The format of the memory is specified by a third party ...
Seth Robertson's user avatar
0 votes
2 answers
76 views

Is volatile necessary when stale values are acceptable?

I have a microservice which receives real time http requests. These requests perform read on a shared Object. This service also has background job which update the reference of the shared object. I ...
Manas Saxena's user avatar
  • 2,181
0 votes
1 answer
55 views

Writing to a volatile field and reading another volatile field: Is the happens-before-rule valid?

Question: Is this statement true: „A write to a volatile field happens-before every subsequent read of that or another volatile field.“ On How to understand happens-before consistent I found the ...
dibo's user avatar
  • 1
5 votes
1 answer
137 views

Assignment operator in C

volatile int lhs = 1; int rhs = 2; int x = 3; x = lhs = rhs; Does an assigment operator return the (typeof lhs)rhs ? or Does it return new, just read value of lhs ? It is important to me since lhs ...
OODAX's user avatar
  • 51
2 votes
2 answers
95 views

Why does my C++ Arduino Program interrupt when I write to an array?

I am trying to create a class that will handle events. class2 represents events, class1 is a structure where events are stored and handled. Here is my simplified code to prove the concept, it doesn't ...
boiledRice's user avatar
4 votes
0 answers
141 views

Why doesn't ConcurrentHashMap of JDK11 need volatile semantics in the methods tabAt and setTabAt?

In ConcurrentHashMap of JDK8, both of the methods tabAtand setTabAt need volatile semantics. static final <K,V> Node<K,V> tabAt(Node<K,V>[] tab, int i) { return (Node<K,V>)...
Oliver's user avatar
  • 41
3 votes
1 answer
97 views

Volatile keyword not able to use properly [closed]

I get the same output with and without the volatile keyword. Have I made a mistake in the code? public class MutliThreadExample extends Thread{ volatile int value; public static void ...
shravan's user avatar
  • 31
1 vote
1 answer
184 views

GLM library deprecation warnings around volatile in C++20

Many operations with volatile were deprecated in c++20 (see https://en.cppreference.com/w/cpp/language/cv) So.... when I used very popular glm library(https://github.com/g-truc/glm) in my project I ...
fsmoke's user avatar
  • 155
1 vote
1 answer
86 views

Do I need to use volatile for mutable object fields with async/await in C#?

I've seen a lot of questions about this area (e.g. https://stackoverflow.com/a/54413147/1756750 , or https://stackoverflow.com/a/55139219/1756750 ), but unfortunately I haven't found anything in ...
Denis's user avatar
  • 3,655
1 vote
1 answer
313 views

Inline asm with input/output operands to update a pointer and update what it pointed to

I'm implementing 3*3 matrix and 3*1 matrix multiplication using RISC-V assembly language, with GNU C inline asm. // description: matrix multiply with two-level for loop #include<stdio.h> int ...
Justdoit's user avatar

1
2 3 4 5
39