how to use volatile key word?

Discussion in 'C++' 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. On Jun 26, 9:13 am, ä¼è™Ž <> wrote:
    > 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 aregister copy? Before a function call? Or before the return statement?


    How do you use the volatile keyword in C++? You don't.

    Volatile has 3 valid, and "obscure", uses in C++ code: signal
    handling, memory mapped IO
    http://en.wikipedia.org/wiki/Memory-mapped_I/O
    and setjmp longjmp stuff.

    The volatile keyword in C and C++, for all standards, is completely
    useless as a portable primitive for threading. Do not use it as such.
    For example, if you tried to use it on any job interview I was giving,
    I would not give you the job. Instead, use POSIX pthreads, windows
    threads, C11 threads, C++11 threads, reliable wrappers on top of those
    such as Boost or ACE (or C11 or C++11), write your own abstraction on
    top of those, or so on. Use mutexes by default. Use weaker atomics
    only when you understand enough to use them correctly.

    To everyone else in this thread: No. Just no. volatile is entirely
    useless as a portable threading primitive in C and C++. If I see any
    code that attempts to use volatile as a portable construct for
    synchronization, I will call it broken, buggy, and in need of
    immediate fixing. However, yes you can use volatile on specific
    hardware with specific compilers to accomplish working code. Note the
    distinction between "specific hardware with specific compilers" vs
    "portable". And if you do use volatile in the non-portable way, at
    least put it inside an inline function or macro which implements the
    "usual semantics" (i.e. C++11 acquire, release, etc.) to keep the evil
    isolated, please.
    Joshua Maurice, Jul 2, 2012
    #2
    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:
    583
    Ulrich Eckhardt
    Jan 11, 2005
  2. M P
    Replies:
    1
    Views:
    454
  3. Sagar
    Replies:
    0
    Views:
    77
    Sagar
    Jan 18, 2006
  4. ä¼è™Ž

    how to use volatile key word?

    ä¼è™Ž, Jun 26, 2012, in forum: C Programming
    Replies:
    4
    Views:
    478
    Tim Rentsch
    Jun 27, 2012
  5. ä¼è™Ž

    how to use volatile key word?

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

Share This Page