volatile variables

Discussion in 'C Programming' started by junky_fellow@yahoo.co.in, May 14, 2007.

  1. Guest

    Guys,

    If I have a global variable, that is shared across multiple
    threads on an SMP environment, then do I need to declare it as
    volatile, so that a particular processor does not store and access its
    vlaue fron the processor registers ?

    thnaks in advance for any help ...
     
    , May 14, 2007
    #1
    1. Advertising

  2. Hari Guest

    je napisao:
    > Guys,
    >
    > If I have a global variable, that is shared across multiple
    > threads on an SMP environment, then do I need to declare it as
    > volatile, so that a particular processor does not store and access its
    > vlaue fron the processor registers ?
    >
    > thnaks in advance for any help ...


    Having volatile type qualifier is mainly to do with the problems that
    are encountered in real-time or embedded systems.

    > so that a particular processor does not store and access its
    > vlaue fron the processor registers


    yes, volatile will use direct access, and probably switch of
    optimization

    > If I have a global variable, that is shared across multiple
    > threads on an SMP environment, then do I need to declare it as
    > volatile


    I think that you must use some thread locking mechanism, not volatile,
    to use global variable with multiple threads. Search net for thread
    locking
    for your platform.

    Best,
    Zaharije Pasalic
     
    Hari, May 14, 2007
    #2
    1. Advertising

  3. Mohan Guest

    wrote:
    > Guys,
    >
    > If I have a global variable, that is shared across multiple
    > threads on an SMP environment, then do I need to declare it as
    > volatile, so that a particular processor does not store and access its
    > vlaue fron the processor registers ?


    Yes, it is required to be a volatile.

    Mohan
     
    Mohan, May 14, 2007
    #3
  4. Eric Sosman Guest

    wrote:
    > Guys,
    >
    > If I have a global variable, that is shared across multiple
    > threads on an SMP environment, then do I need to declare it as
    > volatile, so that a particular processor does not store and access its
    > vlaue fron the processor registers ?


    Strange though it seems, this isn't a question about C.
    Yes, you're asking about a C keyword and its effect on the
    way a C program executes, but it's still not a C question.

    The reason is that C's view of the way a program executes
    is almost entirely single-threaded. There are a few hints --
    volatile, signals -- that other agencies than "the" program
    might be at work, but this part of the picture is blurry and
    indistinct. C describes the effects of function calls, for
    loops, if statements, and so on in terms of a single thread
    of execution. The possibility of other threads isn't ruled
    out altogether, but there's not enough information about how
    they might work to be able to infer much about how they might
    interact with each other.

    ... and in fact, different systems have different ways of
    supporting asynchronous activities, leading to different ways
    in which separate activities interact and coordinate. They
    may start with C, but they build upon it in different -- and
    not entirely compatible -- ways. And your question, phrased
    in terms of a C keyword, is really about those beyond-C matters.

    <off-topic>

    To learn more about the "thread" model of asynchronous
    execution, you may want to read comp.programming.threads for
    a while. Among other things, you will learn that the volatile
    keyword is neither necessary nor sufficient for sharing data
    between multiple threads of execution; it's just irrelevant.

    </off-topic>

    --
    Eric Sosman
    lid
     
    Eric Sosman, May 14, 2007
    #4
  5. "" <> writes:

    > If I have a global variable, that is shared across multiple
    > threads on an SMP environment, then do I need to declare it as
    > volatile, so that a particular processor does not store and access its
    > vlaue fron the processor registers ?


    volatile may work for that or not. It is not the usage for which it has
    been designed (memory mapped IO) and common implementations don't do all
    what is needed for sharing data across threads (not only do a real access
    but also have memory barrier instructions; for memory mapped IO these
    instructions are not needed as in this case the memory region is marked
    non-cachable by the OS).

    Yours,

    --
    Jean-Marc
     
    Jean-Marc Bourguet, May 14, 2007
    #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. Sumukh
    Replies:
    8
    Views:
    3,183
    Roedy Green
    Sep 23, 2005
  2. r_o_h_i_t

    Volatile variables in C!!

    r_o_h_i_t, Feb 14, 2004, in forum: C Programming
    Replies:
    7
    Views:
    11,745
    CBFalconer
    Feb 17, 2004
  3. srinivas reddy

    Volatile variables

    srinivas reddy, Feb 27, 2004, in forum: C Programming
    Replies:
    11
    Views:
    845
    Dan Pop
    Mar 1, 2004
  4. ben
    Replies:
    5
    Views:
    605
    Ulrich Eckhardt
    Jan 11, 2005
  5. jacob navia

    longjmp and volatile variables

    jacob navia, Apr 29, 2006, in forum: C Programming
    Replies:
    6
    Views:
    519
    Ben Pfaff
    May 1, 2006
Loading...

Share This Page