Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Filter by
Sorted by
Tagged with
5 votes
1 answer
146 views

is it valid for std::copy_n to do a memmove when a struct has volatile in it?

I have this kind of structure in my code: struct data { volatile int a; volatile int b; }; // Some hardware that require specific access static data *hardware_data = 0x100000; void function(...
DeadMaX's user avatar
  • 63
5 votes
2 answers
165 views

Is there a difference between the C# Volatile.ReadBarrier vs Volatile.Read?

In the released .NET 10 there is a new method added to the Volatile class with the name ReadBarrier(). And I don't know whether from the compilers perspective there is a difference if I do a read with ...
Dominik Ferencz's user avatar
-1 votes
3 answers
69 views

Trying to read character and turn it into a string

static char* convert_to_string(const volatile uint8_t *ch) { static char read[40]; // static so pointer remains valid after return uint8_t i = 0; while (*ch != ESC || *ch != END_OF_TEXT ||...
Maaz Madha's user avatar
Best practices
0 votes
7 replies
168 views

How to efficiently initialize a volatile struct

I would like use bit-fields to access low level memory. I'm aware of the non-portability of bitfields but they do appear to be consistently implemented on my platform (cortex-m4) on both clang and gcc....
Lee's user avatar
  • 51
2 votes
0 answers
225 views

Difficulty reading volatile memory

I have code that has worked well when compiled with gcc but fails to work correctly when compiled with clang. I tracked down the issue to a read of volatile memory (on a microcontroller). I found by ...
Lee's user avatar
  • 51
4 votes
1 answer
120 views

Interpretation of "refer to" in 6.7.3 7 in the C standard

The standard is quite pedantic in defining any terms used in its text. For example, the term "access" is defined in the context dictionary at the beginning of the standard as modifying or ...
Evgeny Ilyin's user avatar
1 vote
0 answers
99 views

Does critical section protected by semaphore, mutex, etc, implicitly volatile? [duplicate]

Say if I have an array of integers, int array[NUM_ELEMENTS];, access to it is encapsulated as setter and getter function well protected by synchronization such as semaphore, mutex, etc, do I need to ...
PkDrew's user avatar
  • 2,281
2 votes
0 answers
103 views

Does closure variable capture introduce thread safety risks due to field implementation?

Follow-up to: Detailed Explanation of Variable Capture in Closures Given that captured variables are implemented as fields in compiler-generated classes (as explained in the referenced answer), does ...
Jasper's user avatar
  • 39
2 votes
1 answer
131 views

Must T be unqualified in std::allocator<T>?

gcc, clang, and msvc all reject the following code: #include <memory> #include <vector> int main() { auto _ = std::vector<int const>{}; // error auto _ = std::vector<...
xmllmx's user avatar
  • 44.5k
4 votes
2 answers
131 views

Multiple unsequenced modifications following argument evaluation - defined or undefined behavior?

The standard states that the behavior of unsequenced operations that have side effect on the same memory location is undefined [intro.execution§10]. Does the following code have undefined behavior, ...
Amir Kirsh's user avatar
  • 14.4k
2 votes
4 answers
149 views

Declaring volatile pointer to a pointer which points to non-volatile data

When accessing a hardware register in C/C++, I usually create #defines like this: #define HW_REGISTER ((volatile uint16_t *)0xFFFF8A00) I.e. 0xFFFF8A00 is a pointer to a volatile uint16_t value and ...
Miro Kropacek's user avatar
1 vote
2 answers
96 views

CppInsight redundant static_cast of temporary expression to volatile

I was using CppInsight to check an example involing decltype and volatile with the following function. decltype(auto) triple(volatile int &f) { f *= 3; return f; } The insight turned the ...
Askr Askr's user avatar
  • 101
5 votes
3 answers
239 views

Use of volatile with Interlocked.CompareExchange

I came across this blog post by Stephen Toub, demonstrating how to implement an AsyncManualResetEvent: https://devblogs.microsoft.com/dotnet/building-async-coordination-primitives-part-1-...
Codisattva's user avatar
2 votes
3 answers
207 views

The difference between set() and setVolatile of VarHandle

public class MyClass { private volatile Object refValue; private static final VarHandle REF_VALUE_HANDLE; static { try { REF_VALUE_HANDLE = MethodHandles.lookup() ...
Photon's user avatar
  • 29
1 vote
4 answers
162 views

How to make a variable volatile in main loop but not in IR handler

I have a variable which is read from my main loop, and is both read and written from an interrupt handler. The interrupt can change the value at any time, so clearly it needs to be volatile in the ...
Jack B's user avatar
  • 121
4 votes
0 answers
139 views

Why is a volatile symbol placed differently?

When an array is declared with volatile it ends up somewhere different than without: static const char ARRAY[100]; $ nm a.elf | grep ARRAY 00010d00 t ARRAY But: static volatile const char ARRAY[100];...
Caulder's user avatar
  • 459
1 vote
1 answer
100 views

Does assigning a volatile variable to itself suppress compiler optimization in a loop?

I'm working on a delay loop in C and come across an odd case with volatile. Consider this code: void delay(unsigned int count){ volatile unsigned int timer = count; while (timer > 0){ ...
Alphin Thomas's user avatar
5 votes
2 answers
192 views

Is dereferencing a volatile null pointer value undefined behavior?

It seems unclear, but there are claims that dereferencing a nullpointer is undefined behavior: A comment by M.M. This note was in fact removed as the result of DR 1102 with the stated reasoning being ...
lucidbrot's user avatar
  • 6,554
1 vote
1 answer
92 views

Does Volatile.Read() still apply if it is nested in a getter?

Consider this code in C#: private int _backingField; public int SomeNumber => Volatile.Read(ref _backingField); public void Test_1() { var someNumber = Volatile.Read(ref _backingField); /...
user3163495's user avatar
  • 3,968
4 votes
0 answers
126 views

C#'s new "field" keyword: Are "volatile" properties now possible by calling "Volatile.Read(ref field)"?

Traditionally, using the volatile keyword on properties in C# was not possible. In other words, this was not possible: // CS0106: The modifier 'volatile' is not valid for this item public volatile int ...
user3163495's user avatar
  • 3,968
2 votes
1 answer
78 views

Can a @Volatile lateinit var be atomically initialized using a DCL pattern in Kotlin?

Consider the following DCL example written in Kotlin: @Volatile private var property: String? = null private val lock = ReentrantLock() fun singletonValue(): String { if (property == null) { ...
Андрей Щеглов's user avatar
2 votes
2 answers
116 views

Java thread confinement with Executors.newSingleThreadExecutor()

The question is about memory visibility. I have some doubts about whether a Kotlin program like the next would be thread-safe: class MyApi { private val singleThreadExecutor = Executors....
Víctor J García Granado's user avatar
0 votes
0 answers
66 views

Are local variables always volatile? [duplicate]

Are local variables always volatile in C#? In other words, is the volatility of num the same in these two classes: public sealed class ThreadSafeObjectA { private volatile int num; public int ...
user3163495's user avatar
  • 3,968
2 votes
2 answers
65 views

volatile access through cast with volatile-qualified type

I've seen constructs like the following to write to memory-mapped I/O. *((volatile unsigned int *)0xDEADBEEF) = 0x00; But is this guaranteed to be a volatile access?1 I started to think about this ...
andiluk's user avatar
  • 68
1 vote
1 answer
76 views

Volatile and optimization with different compilation units

Please have a look at this more or less standard example that i see people use when talking about the usage of volatile in the context of embedded c++ firmware development on a baremetal target: // ...
IAmPropper's user avatar
1 vote
1 answer
116 views

Perform a function only once in a method call across multiple threads in C#

I have a class from an assembly loaded at runtime that is instantiated using reflection (and therefore using a parameter-less constructor) across multiple threads (using Channels). Each thread ...
Dan Def's user avatar
  • 1,983
4 votes
1 answer
181 views

Is volatile needed with how smart compilers are these days?

Compilers are getting smarter and smarter these days. So, is volatile needed? Compilers are smart enough to ask if we still need volatile ? I tried many scenarios and it seemed like the compiler would ...
Da Wang's user avatar
  • 196
2 votes
3 answers
186 views

Does a C pointer to volatile treat the addressed memory as volatile when it is subscripted?

Consider these two definitions: volatile int a[10]; volatile int *p = a; EDIT: clarify question. If we refer to a[3] that memory location will be treated as volatile storage, that is, the compiler ...
Dan Halbert's user avatar
  • 2,945
2 votes
1 answer
177 views

Java volatile and multithreading: what are the possible outputs for the program?

I've stumbled upon an old university exercise in which the aim was to reason about what can happen in absence of synchronization. Consider the following code: public class Main { public ...
pochopsp's user avatar
  • 1,150
1 vote
1 answer
130 views

If I populate a dictionary and assign it to a field - can I guarantee the field will not contain a half populated dictionary?

In the below code, I populate the contents of the dictionary then assign it to Data. As such, my mental expectation is that Data will contain either null or a Dictionary with two elements in it. But ...
mjwills's user avatar
  • 24.1k
1 vote
2 answers
120 views

Continuous memory reads C compilation optimization questions

I was thinking about the two register timer interview question that goes as follows: There is a hardware memory mapped timer with the value of the timer stored in two registers: one holds the most ...
Adam's user avatar
  • 79
-1 votes
1 answer
85 views

How to prove the volatile visibility guarantee?

I am reading an article about the Java Volatile keyword, got some questions. click here public class SharedObject { public int counter = 0; } Imagine too, that only Thread 1 increments the ...
qwee's user avatar
  • 29
0 votes
1 answer
157 views

Some questions about JUC

public class TestJUC { private int x; public void actor1(){ x = 1; } public void actor2() { System.out.println(x); } } If thread A executes actor1 method and ...
qwee's user avatar
  • 29
1 vote
1 answer
132 views

Accessing one byte of a multi-byte volatile register

Suppose we have a pointer p, of type volatile int*. Assume that this is a currently valid pointer, i.e., reading from *p performs an implementation-defined operation, and is not UB. What can happen if ...
Brian Bi's user avatar
  • 123k
1 vote
1 answer
73 views

About StoreLoad Memory Barrier

I don't understand StoreLoad. Does store1 StoreLoad load2 mean that the CPU's store instruction cannot be reordered after StoreLoad and the load instruction cannot be reordered before StoreLoad? If ...
qwee's user avatar
  • 29
1 vote
1 answer
110 views

How to stop nvcc from reordering clock instructions?

In the following snippet nvcc (CUDA 12.5) 'helpfully' reorders the clock statement. This causes the timings to be off by a factor 26x. #include <cuda.h> #include <stdio.h> __device__ int ...
Johan's user avatar
  • 77.4k
0 votes
1 answer
125 views

LLVM kCFI sanitizer with function of volatile arguments

Here is the minimal reproducible example: // my_func.h typedef volatile struct { int a; } my_vdata_t; typedef struct { int a; } my_data_t; extern void (*vfunc)(my_vdata_t* data); extern void (*...
Simpdanny's user avatar
0 votes
1 answer
160 views

In CUDA, why is performance similar with write-through (with 'volatile') and write-back under light load?

I think under light load, because write back requires writing data to the cache first and waiting for it to be flushed to the global memory, this mode should cause performance waste because there will ...
Shui_'s user avatar
  • 33
0 votes
2 answers
112 views

Passing `volatile` buffers to library functions which do not take `volatile`

I have a multi-processor system with a "shared" memory region used for communication. In some cases one of the processors needs to process some considerably large data in the shared memory ...
Eugene Sh.'s user avatar
  • 18.7k
3 votes
2 answers
149 views

Rationale behind declaring FILE * volatile to stdin in C

I wonder what reason might be to declare FILE *volatile fp as volatile pointer: int main(int argc, char **argv) { int gc; fe_Object *obj; FILE *volatile fp = stdin; fe_Context *ctx = fe_open(...
Mosolov Sergey's user avatar
11 votes
0 answers
202 views

Semantics of volatile _Atomic

Generally _Atomic does not imply semantics of volatile, i.e. operations on the atomic object are not observable side effects that the compiler needs to preserve. As a consequence the compiler can ...
user17732522's user avatar
  • 78.1k
18 votes
1 answer
436 views

Semantics of volatile std::atomic<T>

Generally std::atomic<T> does not imply semantics of volatile, i.e. operations on the atomic object are not observable side effects that the compiler needs to preserve. As a consequence the ...
user17732522's user avatar
  • 78.1k
5 votes
1 answer
444 views

Deeply understanding Volatile and Memory Barriers

I have been reading a ton on this topic over the past view days but would really like clarification on what I learned so far in relations to C# and C. Firstly, Atomicity seems to be fairly intrinsic ...
UnSure's user avatar
  • 148
4 votes
2 answers
292 views

Volatile access of struct member

If accessing a struct member marked as volatile from an interrupt, does the whole chain of access need to be marked as volatile ? For example struct bar { volatile uint16_t a; volatile uint16_t b; ...
Fredrik's user avatar
  • 1,464
0 votes
1 answer
104 views

Do I need to mark my getter/setter and/or class volatile if one of its members is volatile?

I run a complex code on my Teensy board which does beat detection with a microphone and displays some cool LED effects on a bicycle by reacting to them, all in C++ code. But I currently experience a ...
All-in-one Mighty's user avatar
2 votes
1 answer
145 views

How volatile keyword works incase of PG_TRY and PG_CATCH?

Here is the below code which i have tried. Datum sample_func(PG_FUNCTION_ARGS) { int32 var; PG_TRY(); { var = 5; elog(ERROR, "testing"); } PG_CATCH(); { elog(INFO, &...
Sri's user avatar
  • 41
1 vote
2 answers
198 views

Java readwrite concurrency: readwrite lock vs lock + volatile

Assuming that readers = 10 x writers, which of the following solutions is better in terms of throughput. Which solution is better to use in production code? Using single lock for set operation and ...
tinyzero4's user avatar
0 votes
4 answers
453 views

Understanding the volatile Modifier in the Context of x86 Architecture and the Java Memory Model (JMM)

I have a question regarding the Java Memory Model (JMM), particularly in the context of x86 architecture, which I find quite intriguing. One of the most confusing and often debated topics is the ...
Dmytro Kostenko's user avatar
1 vote
0 answers
37 views

how to manage volatile in a method which is marked @Async

In main service class i call another method which annotated with @Async. In input params, i send volatile boolean value. If boolean true, @Async method working, if false, breaks from method. From ...
Nodirbek Sirojiddinov's user avatar
6 votes
3 answers
317 views

Overloading and volatile

How is this expected to work? struct S {}; void foo(volatile S&); void foo(S); int main() { volatile S v; foo(v); } Compilers disagree on it: MSVC accepts the code, while Clang and GCC ...
Dr. Gut's user avatar
  • 3,335

1
2 3 4 5
40