Thread: To Volatile, or not to Volatile?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    You should absolutely use `volatile' for variables that are updated from the great "elsewhere".

    A lot of compiler, at highest optimizations in any event, will still ignore `volatile'. However, the majority of compilers will cache a variable it "knows" isn't updated anyway so `volatile' should only ever help.

    It is true, `volatile' may not prevent this issue in all cases, but without `volatile' you can pretty much bank on having such issues.

    As for everyone saying different things, I can explain part of that. The `volatile' keyword is generally brought up by people trying to force a sequence point. (An example, as you suggested, would be a portable attempt at critical sections. My favorite example is "double checked locking" without serialization primitives for which `volatile' was once thought to solve.) Only a few compilers honor that ideal. The rest of the uncertainty comes from compilers and the standard both of which are seemingly uncertain.

    You aren't trying to introduce a sequence point. You are only saying "Please mister compiler, read this variable every time!".

    By the way, this exact example (memory that might be modified by the great "elsewhere") is one of the few cases where `volatile' is useful. It has nothing to do with threads. If your code was using signal handlers capable of interrupting execution in a way that would write to `isRunning_' you would still need (and want) `volatile'.

    So, yep, `volatile' is perfectly useless for threading (outside of a few very specific cases for a few compilers); that doesn't have anything to do with you because you are doing it to prevent caching. Your use of threads is irrelevant.

    [Edit]
    Full disclosure: this is still not going to work the way you are expecting because your reads and writes aren't atomic simply because of `volatile'.

    In other words, you need `volatile' for the compiler; you also need a mechanism to guarantee atomicity for the processor.

    And some more: if you are using a primitive that is protected from compiler ordering shenanigans by the API level you would not need nor want `volatile'. You would use `volatile' here because you are using a simple integer. If you were using an atomic from a library (like standard POSIX implementations) that atomic would not need `volatile'.

    See? There isn't so much a confusing "Some say this; some say that!" situation as there is a confusing reality.
    [/Edit]

    Soma
    Last edited by phantomotap; 05-11-2012 at 10:12 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Const and volatile
    By GokhanK in forum C Programming
    Replies: 9
    Last Post: 01-16-2011, 06:55 PM
  2. Volatile Variables
    By Gauravmore in forum C Programming
    Replies: 4
    Last Post: 01-11-2011, 08:29 AM
  3. what are the implications of volatile....
    By sanddune008 in forum C Programming
    Replies: 6
    Last Post: 06-29-2010, 04:33 AM
  4. volatile??
    By jacktibet in forum C Programming
    Replies: 2
    Last Post: 05-29-2003, 03:46 PM
  5. volatile keyword help!!!!
    By penney in forum Linux Programming
    Replies: 2
    Last Post: 03-12-2003, 08:09 AM