Volatile

Discussion in 'C' started by arangasoft, Jul 15, 2005.

  1. arangasoft

    arangasoft New Member

    Joined:
    Sep 13, 2004
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Trichy, Tamizh nadu
    hai every one,

    I have some doubt about the keyword 'volatile'.can any one tell me
    about volatile and what is the use of that? and where it is required?.
    how we can use that in efficient manner.I want to know completely
    about the keyword "VOLATILE".

    please reply me soon.

    Arangs
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Hello arangasoft

    Seen you here after quite sometime and something about the volatile keyword.

    If you using a multithreaded program and your one thread is something like
    Code:
    if (myVar ==  NULL)
    {
      SomeFunction();
      if (myVar == NULL)
        ...
    }
    Now consider the situation that SomeFunction is a very long process and in some cases the some other thread can edit the value of myVar then for second condition testing you are in trouble as compiler in process of optimization may not test the condition again and may skip it as always true but if you declare the myVar as volatile then it signals the compiler that its just not the one thread that operates on the variable and so it will not cache the second condition.

    Code:
    volatile MyClass* myVar;
    Thanks and hope it helps
    Shabbir Bhimani
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice