In work today, I came across the volatile keyword in Java. Not being very familiar with it, I found this explaination: http://www.ibm.com/developerworks/java/library/j-jtp06197.html
Given the detail in which that article explains the keyword in question, do you ever use it or could you ever see a case in which you could use this keyword...
I would like to code a little program which visually illustrates the behavior of the 'volatile' keyword. Ideally, it should be a program which performs concurrent access to a non volatile static field and which get incorrect behavior because of that.
Adding the volatile keyword in the same program should fix the problem.
That somethin...
Let's say that a class has a public int counter field that is accessed by multiple threads. This int is only incremented or decremented.
To increment this field, which approach should be used, and why?
lock(this.locker) this.counter++;
Interlocked.Increment(ref this.counter);
Change the access modifier of counter to public volatile
...
In C and C++ a variable can be marked as volatile, which means the compiler will not optimize it because it may be modified external to the declaring object. Is there an equivalent in Delphi programming? If not a keyword, maybe a work around?
My thought was to use Absolute, but I wasn't sure, and that may introduce other side effects....
I have used a static global variable and a static voltalile variable in file scope,
both are updated by an ISR and a main loop and main loop checks the value of the variable. here during optimization neither global vriable nor the volatile variable are optimized. So instead of using a volatile variable a global variable solves the probl...
Hi,
for a paper I'm looking for an real-life C function which uses volatile variables. That in itself is not hard to find, but I am looking for a function in which the value of the volatile variable must change during the course of the execution of the function, for a particular branch of the function to be reached. Something like thi...
I have a reference-type variable that is readonly, because the reference never change, only its properties. When I tried to add the volatile modifier to it the compiled warned me that it wouldn't let both modifiers apply to the same variable. But I think I need it to be volatile because I don't want to have caching problems when reading ...
The following code
using System.Threading;
class Test
{
volatile int counter = 0;
public void Increment()
{
Interlocked.Increment(ref counter);
}
}
Raises the following compiler warning:
"A reference to a volatile field will not be treated as volatile"
Am I doing something wrong here to raise this warning? Why...
As only reference types and a few primitives (including float, but not double, I'm not sure the reason why, I'm happy to hear why) can be declared as volatile, if I wrap a double in a class then declare it as volatile (as below), will the double property be 'read write' thread safe as any other volatile, or should I still be looking at l...
Is there a way to declare an iterator which is a member variable in a class and that can be incremented using a member function even though the object of that class is const.
...
Consider the following:
volatile uint32_t i;
How do I know if gcc did or did not treat i as volatile? It would be declared as such because no nearby code is going to modify it, and modification of it is likely due to some interrupt.
I am not the world's worst assembly programmer, but I play one on TV. Can someone help me to understan...
I was reading about volatile member function and came across an affirmation that member function can be both const and volatile together. I didn't get the real use of such a thing. Can anyone please share their experience on practical usage of having member function as const and volatile together.
I wrote small class to test the same:
...
New to this website, so let me know if I'm not posting in an accepted manner.
I've frequently coded something along the lines of the sample below(with stuff like Dispose ommited for clarity. ). My question is, are the volatiles needed as shown? Or does the ManualResetEvent.Set have an implicit memory barrier as I've read Thread.Start...
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, except if the instructions are re-ordered or done in registers. I know I can use memory barriers to stop re-ordering, but how can I ensure values ...
int main(int argc, char ** argv)
{
int i = 0;
i = i++ + ++i;
printf("%d\n", i); // 3
i = 1;
i = (i++);
printf("%d\n", i); // 2 Should be 1, no ?
volatile int u = 0;
u = u++ + ++u;
printf("%d\n", u); // 1
u = 1;
u = (u++);
printf("%d\n", u); // 2 Should also be one, no ?
register int v = 0;
v...
Can anyone explain to me what the benefits and and drawbacks of the two different approaches are?
...
How do you declare a particular member of a struct as volatile?
...
I have an issue with some low level code I am writing, I need to use objects as volatile, but it is not necessarily so that I want the types to be declared as volatile (for reusability reasons). I can however define pointer to a qualified variant of a structure as detailed in the following segment.
struct x {
int bar;
};
struct x foo...
Possibly similar question:
http://stackoverflow.com/questions/106591/
Today I was debugging my game; It had a very difficult threading problem that would show up every few minutes, but was difficult to reproduce. So first I added the synchronized keyword to each of my methods. That didn't work. Then I added the volatile keyword to ever...
we're running into performance issues, and one potential culprit is a centralized use of a volatile singleton. the specific code is of the form
class foo {
static volatile instance;
static object l = new object();
public static foo Instance {
if (instance == null)
lock(l) {
if (instance == null)
instan...