how to use volatile key word?

Discussion in 'C Programming' started by ä¼è™Ž, Jun 26, 2012.

  1. ä¼è™Ž

    ä¼è™Ž Guest

    how to use volatile key word?

    Assume I have a global variable shared by two or more tasks. Do I need to declare it as volatile?

    When will a non-volatile global variable be written back to memory from a register copy? Before a function call? Or before the return statement?
     
    ä¼è™Ž, Jun 26, 2012
    #1
    1. Advertising

  2. ä¼è™Ž

    Xavier Roche Guest

    Le 26/06/2012 18:10, ä¼è™Ž a écrit :
    > how to use volatile key word?


    [ I can not find the exact C spec extract for volatile ; maybe someone
    will complete this approximative message ]

    volatile is meant to access part of memory which can be modified outside
    the scope of the current thread (by another thread, for example, or by a
    signal handler in the same thread ; it can be mapped memory, or
    hardware-mapped memory too)

    "volatile" in C is not intended for synchronization purpose (ie. unlike
    in java). It will _not_ cause any memory barriers, nor it will ensure
    operations are atomical.

    You can see it as a strong hint to the compiler: "do not attempt to make
    optimizations on this variable" ; such as in this case:

    extern int spin = 0;

    void wait_spin() {
    while(!spin) ;
    }

    void set_spin() {
    spin = 1;
    }

    In this case, a sane compiler will optimize wait_spin() into:

    void wait_spin() {
    if (!spin) {
    for(;;) ;
    }
    }

    ... leading to an infinite loop.

    By declaring spin "volatile", the compiler will not assume that "spin"
    can not be modified inside the scope if wait_spin().

    > Assume I have a global variable shared by two or more tasks. Do I need to declare it as volatile?


    No. You should properly synchronize it, for example using mutexes.

    > When will a non-volatile global variable be written back to memory from a register copy? Before a function call? Or before the return statement?


    If it is volatile, it will be read/written immediately, the compiler is
    not supposed to alias it to a register. (except if the operation is not
    atomical and is processed using a temporary register). However, once
    again, synchronization and side effects propagating will be totally
    unspecified.
     
    Xavier Roche, Jun 26, 2012
    #2
    1. Advertising

  3. ä¼è™Ž

    Eric Sosman Guest

    On 6/26/2012 12:10 PM, ä¼è™Ž wrote:
    [... reformatted for line length ...]
    > how to use volatile key word?
    >
    > Assume I have a global variable shared by two or more tasks.
    > Do I need to declare it as volatile?


    That will depend on what you mean by "tasks," and on how you
    make the variable "shared." If the two "tasks" are executing
    different programs, anything you "share" between them is probably
    in a shared memory segment or something of that kind, and has its
    own rules for ensuring consistency. If they are "threads" in the
    same program, `volatile' is (probably) neither necessary nor
    sufficient to guarantee consistency; I suggest you seek more
    information in comp.programming.threads.

    (The very latest "C11" version of the C Standard includes
    features to support multi-threaded programming, and may have more
    to say on the matter. However, I have not yet studied C11's
    threading stuff in detail -- also, C11 is so new that few compilers
    support it yet.)

    > When will a non-volatile global variable be written back to
    > memory from a register copy? Before a function call? Or before
    > the return statement?


    It will be written back to memory when the implementation
    chooses. That's really all that can be said.

    Note that in a multi-threaded context, "written back to
    memory" does not mean "Seen in a consistent state by all threads."
    Most threading models require some kind of explicit synchronization
    between threads that read and write the same object; merely "T1
    wrote it, then eight nanoseconds elapsed before T2 read it" is
    not enough to guarantee that T2 sees what T1 wrote, nor that T2
    sees anything sensible at all. Again, I'll refer you to
    comp.programming.threads.

    --
    Eric Sosman
    d
     
    Eric Sosman, Jun 26, 2012
    #3
  4. ä¼è™Ž

    Jens Gustedt Guest

    Am 26.06.2012 18:10, schrieb ä¼è™Ž:
    > how to use volatile key word?
    >
    > Assume I have a global variable shared by two or more tasks. Do I
    > need to declare it as volatile?
    >
    > When will a non-volatile global variable be written back to memory
    > from a register copy? Before a function call? Or before the return
    > statement?


    Using it in such a context is not sufficient. Even with volatile there
    would be no guarantee that the compiler would load a consistent value
    from memory. E.g in extreme cases it could load the upper half of a
    variable in one load and and the lower half with another. Your program
    then could see a mix of two different values.

    The feature that you would be needing is C11's _Atomic and not
    volatile. Many compilers already provide similar features as
    extensions. P99 supports part of the C11 syntax on top of gcc and
    cousins.

    Jens
     
    Jens Gustedt, Jun 26, 2012
    #4
  5. ä¼è™Ž

    Tim Rentsch Guest

    <> writes:

    > how to use volatile key word?
    >
    > Assume I have a global variable shared by two or more tasks. Do I
    > need to declare it as volatile?
    >
    > When will a non-volatile global variable be written back to memory
    > from a register copy? Before a function call? Or before the return
    > statement?


    If your intention is to write code that runs on several platforms,
    using volatile almost certainly isn't the right way to do that.
    It is possible using volatile will help, but there is no guarantee
    of that for multi-platform or cross-platform code. Probably though
    this isn't what you're setting out to do.

    If you are writing code for a specific platform, or perhaps for a
    small number of specific platforms, it's best to consult the
    platform-specific documentation for how to do what you want.
    That way you'll be better informed, and the information you
    get is much more likely to be a reliable indicator of what's
    needed to do what you want. If that documentation says use
    volatile, then use volatile; if it says something else, do
    that instead. Also that documentation should let you answer
    your other questions.
     
    Tim Rentsch, Jun 27, 2012
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ben
    Replies:
    5
    Views:
    598
    Ulrich Eckhardt
    Jan 11, 2005
  2. M P
    Replies:
    1
    Views:
    484
  3. Sagar
    Replies:
    0
    Views:
    86
    Sagar
    Jan 18, 2006
  4. ä¼è™Ž

    how to use volatile key word?

    ä¼è™Ž, Jun 26, 2012, in forum: C++
    Replies:
    1
    Views:
    569
    Joshua Maurice
    Jul 2, 2012
  5. ä¼è™Ž

    how to use volatile key word?

    ä¼è™Ž, Jun 26, 2012, in forum: C++
    Replies:
    24
    Views:
    1,036
    Philip Lantz
    Aug 3, 2012
Loading...

Share This Page