12

What is the difference in volatile in c# and c? I was asked this in one interview.

Ian Ringrose
  • 51,220
  • 55
  • 213
  • 317

2 Answers2

16

The article mentioned in this blog post by Herb Sutter explains the things concisely and clearly and compares the usage and meaning of volatile in C, C++, Java and C#.

There are a couple of good questions and answers on this very site also:

EDIT: to not confuse anyone here is the "exact link" to the DDJ article mentioned in the initial link(which is a link to Herb Sutter's blog).

Also this article by Nigel Jones explains the volatile keyword in the context of embedded C programming. As this question seems to have popped up in an interview this other article by the same author is one of my favorites("exact link") and has another good explanation of volatile in the C world.

Community
  • 1
  • 1
celavek
  • 5,575
  • 6
  • 41
  • 69
  • by following your link i could not understand the difference – Luis Filipe Aug 03 '11 at 08:31
  • 1
    @Luis Filipe what exactly you did not understand? Just to summarize what the article presents: in Java/.NET(C#) volatile is used for lock free programming whereas in C/C++ is used for "Unusual memory semantics" (describing HW registers, memory at more than one address) – celavek Aug 03 '11 at 08:36
  • You have to follow through to the actual ddj article. Don't know if that's easy to link to (adverts). – H H Aug 03 '11 at 08:36
  • @celavek Thanks for your explanation. I realize that my lack of understanding is due to my ignorance in the subject. I'll learn it when i have the time. – Luis Filipe Aug 03 '11 at 10:34
  • @Luis Filipe I was not pejorative in any way in my comment, just trying to get more info from you to understand where the misunderstanding was. If you give more details about what exactly is not understandable then maybe I or someone else more knowledgeable might help you. – celavek Aug 03 '11 at 10:40
  • @celavek I didn't understand it as pejorative. I honestly think the best is to read about it when i have the time and then, if doubts remain, i'll look for community help. Again, thanks. – Luis Filipe Aug 03 '11 at 15:17
2

In C volatile tells the compiler not to optimize access to a variable:

int running = 1;
void run() {
    while (running) {
        // Do something, but 'running' is not used at all within the loop.
    }
}

Normally the compiler may translate 'while (running)' in this case to just 'while (1)'. When the 'running' variable is marked as volatile, the compiler is forced to check the variable each time.

Important is to understand that for C 'volatile' only restricts the compiler to optimize, while your hardware (i.e. the CPU caches, instruction pipeline, etc) may still reorder memory access. There is no way your C compiler will tell your hardware not to optimize. You have to do it yourself (e.g. by using a memory barrier).

As far as I know (but I'm not completely sure) the C# specification takes this a bit further:

  • If you write to a volatile variable, it is guaranteed that all memory accesses that you do before it are completed.
  • If you read from a volatile variable, it is guaranteed that all memory accesses that you do after it are not completed before the read of the volatile varable is completed.
Bart
  • 1,633
  • 14
  • 21