Skip to main content
Filter by
Sorted by
Tagged with
Best practices
1 vote
5 replies
139 views

I have a regular byte buffer declared in file scope: static unsigned char buffer[32]; At some point, the function to start a DMA read operation into this buffer is called: void func(void) { ...
Evgeny Ilyin's user avatar
0 votes
1 answer
88 views

ThreadA getfield Stop.flag is false. At a certain point, another thread modifies Stop.flag to true, but ThreadA getfield Stop.flag always returns false. I don’t understand: Under the CPU MESI protocol,...
杨尚山's user avatar
2 votes
1 answer
113 views

In the C standard (at least, I'm looking at draft C23 WG14/N3088), section 6.7.3 Type Qualifiers states: 7 If an attempt is made to modify an object defined with a const-qualified type through the ...
Evgeny Ilyin's user avatar
6 votes
1 answer
168 views

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
  • 73
5 votes
2 answers
195 views

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
0 votes
3 answers
72 views

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
173 views

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

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
129 views

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

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,301
2 votes
0 answers
104 views

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
135 views

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.6k
4 votes
2 answers
133 views

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
151 views

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
105 views

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
242 views

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
210 views

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
163 views

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
144 views

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
102 views

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
195 views

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,552
1 vote
1 answer
92 views

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
130 views

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
83 views

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
117 views

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
67 views

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
70 views

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
77 views

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
117 views

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,987
4 votes
1 answer
183 views

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
189 views

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
178 views

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
136 views

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

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
91 views

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
158 views

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
134 views

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
81 views

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
111 views

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
127 views

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
173 views

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
114 views

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
151 views

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
209 views

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
437 views

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
455 views

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
296 views

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
109 views

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

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
199 views

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

1
2 3 4 5
41