volatile

Do you ever use the volatile keyword in Java?

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...

How to illustrate usage of volatile keyword in C#

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...

Volatile vs. Interlocked vs. lock

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 ...

Does Delphi have any equivalent to C's volatile variable?

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....

What is the difference between a static global and static volatile variable?

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...

Example C Function using volatile variables

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...

Why readonly and volatile modifiers are mutually exclusive?

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 ...

"A reference to a volatile field will not be treated as volatile" implications

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...

C# volatile double

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...

how to declare volatile iterator in c++

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. ...

How do I know if gcc agrees that something is volatile?

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...

when should a member function be both const and volatile together?

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: ...

Does an EventWaitHandle have any implicit MemoryBarrier?

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...

How do I specify the equivalent of volatile in VB.net?

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 ...

Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc...)

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...

Java seems to support volatile fields of type long, while C# does not - What are the reasons behind this?

Can anyone explain to me what the benefits and and drawbacks of the two different approaches are? ...

In C, how do you declare the members of a structure as volatile?

How do you declare a particular member of a struct as volatile? ...

Volatile semantics in C99

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...

Why aren't variables in Java volatile by default?

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...

What is the cost of the volatile keyword in a multiprocessor system?

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...