volatile Questions
3
Solved
I am working on someone's code and came across the equivalent of this:
for (int i = 0; i < someVolatileMember; i++) {
// Removed for SO
}
Where someVolatileMember is defined like this:
priv...
4
Solved
https://msdn.microsoft.com/en-us/magazine/jj883956.aspx
Consider the polling loop pattern:
private bool _flag = true;
public void Run()
{
// Set _flag to false on another thread
new Thread(...
Ruberta asked 1/10, 2018 at 12:44
2
Look at this little snippet:
struct A {
virtual ~A() { }
};
struct B { };
bool fn() {
A *volatile a = new A;
return dynamic_cast<B *>(a);
}
Is the compiler allowed to remove the dyna...
Hurter asked 27/9, 2018 at 21:38
2
Solved
I'm working on a embedded system, where some calibration data is stored in the flash memory. The calibration data is stored in a struct which is placed in a special section that the linker knows to...
1
Solved
Consider the following piece of code (which isn't quite what it seems at first glance).
static class NumberContainer {
int value = 0;
void increment() {
value++;
}
int getValue() {
return...
2
Solved
In C++11 standard the machine model changed from a single thread machine to a multi threaded machine.
Does this mean that the typical static int x; void func() { x = 0; while (x == 0) {} } example...
2
Solved
Official notes say, that
Writing to a volatile field has the same memory effect as a monitor release, and reading from a volatile field has the same memory effect as a monitor acquire.
and
...
Candracandy asked 5/8, 2018 at 10:26
2
Solved
I hava following code to test volatile. bEnd and nCount are defined volatile.
nCount = 0, bEnd = false
The Writer thread will set
nCount = 100, bEnd = true
The Reader thread read these viri...
Tyishatyke asked 8/8, 2018 at 8:44
4
Solved
Here's the problem: your program temporarily uses some sensitive data and wants to erase it when it's no longer needed. Using std::fill() on itself won't always help - the compiler might decide tha...
1
Solved
I noticed that most member functions of std::atomic<T> types are declared twice, once with the volatile modifier and once without (example)). I checked the source code of the G++ standard lib...
6
Is the compiler allowed to optimize this (according to the C++17 standard):
int fn() {
volatile int x = 0;
return x;
}
to this?
int fn() {
return 0;
}
If yes, why? If not, why not?
Here...
Laughry asked 23/7, 2018 at 6:1
2
Where does the standard define that a volatile variable can change
undetected?
I've found two normative text which are about volatile:
intro.execution/7:
Reading an object designated by a vol...
Alitaalitha asked 23/7, 2018 at 12:19
3
Solved
(Before I start: I know there are existing questions on this topic, but none I've found answer why this is an issue. I do it regularly and would like to know if I am creating potential problems.)
...
6
Solved
I'm attempting to write a lock-free version of a call queue I use for message passing. This is not for anything serious, just to learn about threading.
I'm relatively sure my code is correct, exce...
Moneylender asked 30/5, 2009 at 6:15
2
Solved
We consider the following program, that is just timing a loop:
#include <cstdlib>
std::size_t count(std::size_t n)
{
#ifdef VOLATILEVAR
volatile std::size_t i = 0;
#else
std::size_t i = 0...
Raddi asked 19/6, 2018 at 19:32
13
Solved
I understand that volatile informs the compiler that the value may be changed, but in order to accomplish this functionality, does the compiler need to introduce a memory fence to make it work?
Fr...
Teage asked 10/10, 2014 at 19:51
2
Solved
I am quoting from Oracle's Java documentation on Atomic Access
Reads and writes are atomic for reference variables and for most primitive variables (all types except long and double).
Reads a...
Weird asked 16/5, 2018 at 15:10
4
Solved
When you use the -O0 compiler flag in C, you tell the compiler to avoid any kind of optimization. When you define a variable as volatile, you tell the compiler to avoid optimizing that variable. Ca...
Tripping asked 15/5, 2018 at 11:20
1
Solved
Consider this example:
volatile unsigned int x;
unsigned int y;
void f() {
x /= 2;
}
void g() {
y /= 2;
}
When compiled with -Os, clang-6.0 produces on x64 for both f and g the same shrl <...
7
Solved
I have read "When to use 'volatile' in Java?" but I'm still confused. How do I know when I should mark a variable volatile? What if I get it wrong, either omitting a volatile on something tha...
Bertram asked 15/8, 2010 at 18:36
4
I am reading that the volatile keyword is not suitable for thread synchronisation and in fact it is not needed for these purposes at all.
While I understand that using this keyword is not sufficie...
Glooming asked 11/2, 2016 at 17:19
3
Solved
struct FOO{
int a;
int b;
int c;
};
volatile struct FOO foo;
int main(void)
{
foo.a = 10;
foo.b = 10;
foo.c = 10;
struct FOO test = foo;
return 0;
}
This won't compile, because
struct...
1
Solved
I stumbled upon this article on IBM - developerworks, and the code they posted had me raise some questions:
Why is the building of the local variable Map wrapped within a synchronized block? Note...
Anemone asked 16/3, 2018 at 21:22
1
Solved
Consider this following piece of code:
struct S{
int i;
S(int);
S(const volatile S&);
};
struct S_bad{
int i;
};
volatile S as{0};
volatile S_bad as_bad{0};
volatile int ai{0};
void t...
Foreshow asked 4/3, 2018 at 11:26
3
I know how volatile works in C++. However, I'm still feeling confused about why we need volatile. Only reason I think is we need listen whether the variable changed, as the code shows below:
volat...
© 2022 - 2024 — McMap. All rights reserved.