CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    [RESOLVED] volatile fields

    Im pretty sure I have a firm understanding of what making a class field volatile does. But Im just looking for some confirmation other than MS's fancy smancy explanation.

    Basically when you add the keyword volatile to a field, it makes that variable "auto-locking" when it is accessed. So if you have two threads trying to access the same field, one thread will wait until the other thread has finished with accessing the field before it attempts to access the field, correct? volatile is essentially the same thing as locking the field for the duration of time that it is accessed, then unlocking it when the thread is done accessing it, am I right?
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  2. #2
    Join Date
    May 2007
    Posts
    1,546

    Re: volatile fields

    Quote Originally Posted by RaleTheBlade View Post
    Im pretty sure I have a firm understanding of what making a class field volatile does. But Im just looking for some confirmation other than MS's fancy smancy explanation.

    Basically when you add the keyword volatile to a field, it makes that variable "auto-locking" when it is accessed. So if you have two threads trying to access the same field, one thread will wait until the other thread has finished with accessing the field before it attempts to access the field, correct? volatile is essentially the same thing as locking the field for the duration of time that it is accessed, then unlocking it when the thread is done accessing it, am I right?
    Nope, what it basically does is ensures that every call to that variable results in an actual read of the memory address. It doesn't affect thread safety as such. To give the behaviour you want, you'd need a lock (mutex or whatever).

    For example, the CLR might optimise:

    bool a = MyBool;
    bool b = MyBool;

    into

    bool a = MyBool;
    bool b = a;

    However if MyBool was marked with volatile, this optimisation wouldn't happen. Thats the simplest example I can think of to demonstrate the point. The idea is that if the field is 'volatile', it can change *at any stage*, so you must always read from the actual memory address.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  3. #3
    Join Date
    Dec 2005
    Location
    England
    Posts
    86

    Re: volatile fields

    It's used, for example, for memory-mapped fields. Basically, it indicates a field that can change even if not assigned to, and ensures that the compiler won't apply any optimisations that rely upon that assumption.

  4. #4
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: volatile fields

    Huh, what I got from the convoluted MSDN documentation was that it allowed an implicit "lock" of the variable whenever it was in use. Of course the MSDN documentation also states that

    Code:
    NotifyIcon.ShowBalloonTip(int timout)
    takes a timeout argument in milliseconds, but in the example it shows

    Code:
    notifyIcon1.ShowBalloonTip(20);
    Seems kinda hokey to me...
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  5. #5
    Join Date
    May 2007
    Posts
    1,546

    Re: volatile fields

    Quote Originally Posted by RaleTheBlade View Post
    Huh, what I got from the convoluted MSDN documentation was that it allowed an implicit "lock" of the variable whenever it was in use.
    I can definitely see how you can make that mistake. MSDN states:

    The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock statement to serialize access. Using the volatile modifier ensures that one thread retrieves the most up-to-date value written by another thread.
    Volatile performs a completely different task to a lock statement. A lock statement guarantees that only 1 thread can operate inside the lock at any one time. The volatile keyword guarantees that you will always read *directly* from memory every time you access a variable*. So even if you are using lock, you may still need 'volatile' and vice versa.

    *I think it also gives guarantees that loads/stores will not be 'optimised' by changing the order in which they're executed.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  6. #6
    Join Date
    Dec 2005
    Location
    England
    Posts
    86

    Re: volatile fields

    Quote Originally Posted by RaleTheBlade View Post
    Of course the MSDN documentation also states that

    Code:
    NotifyIcon.ShowBalloonTip(int timout)
    takes a timeout argument in milliseconds, but in the example it shows

    Code:
    notifyIcon1.ShowBalloonTip(20);
    Seems kinda hokey to me...
    It's obviously because MSDN readers are incredibly smart and can read a tooltip in a fiftieth of a second.

  7. #7
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: volatile fields

    Quote Originally Posted by Twey View Post
    It's obviously because MSDN readers are incredibly smart and can read a tooltip in a fiftieth of a second.
    hahahaha... kudos. I'm marking this thread resolved, thanks for clearing it up you guys
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured